constructor-related issues?

Source: Internet
Author: User
Constructor problem???
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 () {
Many of the following methods are used to the object of a, so I in the Class B constructs a direct object, convenient to call
$a =new A ();



The member method of the following B begins to invoke 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.

Share to:


------Solution--------------------
Scope Problem of variables
$this->a = new A ();
$mes = $this->a->mes;

  • 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: info-contact@alibabacloud.com 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.