$ This-> construct ($ x) in class; meaning

Source: Internet
Author: User
$ This-& gt; construct ($ x); meaning class aaa {
Var $ B;
Function _ construct ($ x ){
$ This-> B = $ x;
}
Function B ($ x ){
$ This->__ construct ($ x );
}
Function c {...}
Function d (){...}
}

$ K = new aaa (x );
$ Html_text = $ k-> d ();

In the above class
Function B ($ x ){
$ This->__ construct ($ x );
}
Why can I omit it? Who can explain it in detail?


Reply to discussion (solution)

_ Construct is a constructor.
The new classname is automatically executed. The main function is to initialize the instance as you do.
If you have nothing to do, you can omit it.

Function B ($ x ){
$ This->__ construct ($ x );
}


Change

Function B ($ x ){
$ This-> B = $ x;
}
You will understand.

_ Construct constructor
It is automatically called when the class is instantiated. it is usually used for initialization.

Correct it
The original code of function B ($ x) {$ this->__ construct ($ x);} in the class is as follows:
Function aaa ($ x) {$ this->__ construct ($ x);} // correct the same name of the function and class aaa. // this sentence is not quite understandable,

In my understanding
When $ k = new aaa ("x") is used; during instantiation, function _ construct ($ x) {$ this-> B = $ x;} is executed ;} to complete the assignment;
However, other functions c {...} and function d () {...} in the class will not be executed,


The problem is:
The only function aaa with the same name as the class has only one $ this->__ construct ($ x) in the class. what does it do?
If $ k = new aaa ("x") at this time, will the instantiation be executed?
I tried it and it seems that it was not executed, because I added echo "x" to it and changed it to function aaa ($ x) {$ this->__ construct ($ x ); echo "aaaaa"; echo $ x ;}
$ K = new aaa ("x"); no output is available after instantiation. You still need to use $ k-> aaa (55555) for output after instantiation.
So I don't understand the vunction of $ this->__ construct ("the same parameter during instantiation") and what special functions does it play?

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.