Constructor question ???

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

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.