Presumably, the class B method uses a lot of object instances of Class A, so I instantiate class A directly inside the class B constructor.
Class A{public $mes = "OK";} Class B{public function __construct () {///There are many methods to use the object of a, so I in the class B construction method directly within the object, convenient to call $a=new a ();}// The member method of the following B starts calling the method of the A object public function test () {$mes = $a->mes; The code runs here to prompt $ A to not know what the thing is, that is, there is no instantiation, but I did not do it on the constructor? It doesn't seem to work. Echo $mes;} $b =new B (); $b->test ();}
I instantiate the A object in the constructor of B, the member method of B is not used, what's going on? I have a lot of members behind the method is to use a object Ah, can not be a one to instantiate oh.
Reply to discussion (solution)
Scope Problem of variables
$this->a = new A ();
$mes = $this->a->mes;
The above running results prompt:
notice:undefined variable:a in C:\php\apache\htdocs\test.php on line 18
Notice:trying to get property of Non-object in C:\php\apache\htdocs\test.php on line 18
Scope Problem of variables
$this->a = new A ();
$mes = $this->a->mes;
Cow! Gave away! Thank!