Constructor question ??? This is probably the case. Class B methods use A large number of Class A object instances, so I directly instantiate Class A in Class B constructor.
Class A {public $ mes = "OK";} class B {public function _ construct () {// The following methods use the object, so I directly set an object in the class B constructor to call $ a = new ();} // The Following B member method starts to call the method public function test () {$ mes = $ A-> mes of object; // when the code runs here, it prompts $ a does not know what it is, that is, it is not instantiated, but I did not do it in the constructor? Echo $ mes;} $ B = new B (); $ B-> test ();}
The A object I instantiate in the Constructor B cannot be used in the Member method of B. What is the problem? I have many member methods that use object a. It is impossible to instantiate objects one by one.
Reply to discussion (solution)
Scope of variables
$ This-> a = new ();
$ Mes = $ this-> a-> mes;
The preceding running result prompts:
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 of variables
$ This-> a = new ();
$ Mes = $ this-> a-> mes;
Niu! Just a few words! Thank you!