PHP - 访问外部对象的属性和方法

时间:2012-10-21 09:09:10

标签: php

我是php的新手,我想知道为什么这不起作用:

class A {
      private $containigObject;
      public function __construct($containigObject){
        $this->containigObject = $containigObject;
      }
      public function functionThatAccessOutsideObject(){
        //...
        $this->containgObject->b = 2;
        //...
      }
}

class B {
      public $b = 1;
      public function someFunction(){
        //...
        $a = new A($this);
        $a->functionThatAccessOutsideObject();
        //...
      }
}

$b = new B();
$b->someFunction();

我正在尝试使用A类中B类的属性和方法

1 个答案:

答案 0 :(得分:0)

class A {
  private $**containigObject**;
  public function __construct($containigObject){
    $this->containigObject = $containigObject;
  }
  public function functionThatAccessOutsideObject(){
    //...
    $this->**containgObject**->b = 2;
    //...
  }

}