Constructor problem???

Source: Internet
Author: User
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!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.