dongxi0523 2016-10-30 20:11
浏览 25

对象覆盖PHP

I've two classes which extend another class.

But when the second object is instantiated gets the definitions of the first.

Object B gets the definitions of the object A

Class A

class a extends Connection{

    public function __construct() { parent::__construct("TOKENS");}

    public function helloA(){
        echo "A";
    }
}

Class B

class B extends Connection{

    public function __construct() { parent::__construct("USERLEVEL"); }

    public function helloB(){
        echo "B";
    }
}

Class Connection

class Connection extends dbData{

    private $table;

    function __construct($name){
        $this->setTable($name);
        parent::__construct();
    }

    public function setTable($name){
        $this->table = parent::getTable($name);
    }

    public function connectionInsert($info){
        return parent::insert($this->table, $info);
    }

    public function connectionSelect($where="", $bind="", $fields="*"){
        return parent::select($this->table, $where, $bind, $fields);
    }

    public function connectionDelete($where, $bind=""){
        return parent::delete($this->table, $where, $bind);
    }

    public function connectionRun($sql, $bind=""){
        return parent::run($sql, $bind);
    }

    public function connectionEdit($info, $where, $bind){
        return parent::update($this->table, $info, $where, $bind);
    }


}

Testing File

   $a = new a();
   print_r($a);
   $b = new b();
   print_r($b);

Results:

a Object ( [table:Connection:private] => tokens [error:Db:private] => [sql:Db:private] => [bind:Db:private] => [errorCallbackFunction:Db:private] => [errorMsgFormat:Db:private] => )
a Object ( [table:Connection:private] => tokens [error:Db:private] => [sql:Db:private] => [bind:Db:private] => [errorCallbackFunction:Db:private] => [errorMsgFormat:Db:private] => ) 

But if i do a print_r directly of the new A() or new B()

print_r(new a());
print_r(new b());

The result is what i want to get.

a Object ( [table:Connection:private] => tokens [error:Db:private] => [sql:Db:private] => [bind:Db:private] => [errorCallbackFunction:Db:private] => [errorMsgFormat:Db:private] => )
B Object ( [table:Connection:private] => us_level [error:Db:private] => [sql:Db:private] => [bind:Db:private] => [errorCallbackFunction:Db:private] => [errorMsgFormat:Db:private] => ) 

Even when i'm trying to access the functions of each class, suddenly i've got the error saying that function doesn't exist on that class.

Testfile

$a = new a();
print_r($a->helloA());

$b = new b();
echo "</br>";
print_r($b->helloB());

Error Message:

Fatal error: Call to undefined method a::helloB() 

So, at this point i've no clue why i'm getting the objects overwritten.

Anybody can help?

Thanks

  • 写回答

1条回答 默认 最新

  • drag2458 2016-10-30 20:39
    关注

    I copy/pasted your code in my IDE and ran but no problem here! so it's very strange that when you create object B, you get error that object A method (a::helloB()) does'n exist! If you put whole codes (inclusive dbData) i am willing to try it again..

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)