PHP _ construct constructor usage

Source: Internet
Author: User

From http://ribeter267.blog.163.com/

"

The constructor appeared in PhP4, and then added an analysis structure in PhP5. This makes PHP more object-oriented. In PhP4, constructor uses a function with the same name as the class to construct this action. For example:
<? PHP/** Myclass. PHP */class myclass {function myclass {print "this is myclass \ n" ;}}// create a new instance $ OBJ = new myclass ();?>

Replace the aboveCodeSave as myclass. php. After running, the words "this is myclass" will be printed. This is the PhP4 Constructor (of course, PhP5 can also be written in this way for compatibility ).
In PhP5, We have specialized constructor and destructor. _ Construct () and _ destruct (). Rewrite myclass. php again. <? PHP /** Myclass. PHP */class myclass {function _ construct () {print "constructor \ n";} function _ destruct () {print "destroying \ n ";}} // create a new instance $ OBJ = new myclass ();?>
After saving the file, it is found that "constructor destroying" is printed ". It indicates that the structure and analysis structure have indeed occurred.
So far, there are no problems. In the PhP5 manual, there are still some instructions on the use of _ construct. As follows:
To implement backward compatibility (PHP 4), If PHP 5 cannot find the _ construct () function in the class, it will try to find the old constructor, that is, a function with the same name as a class. Therefore, the only situation that causes compatibility problems is that the class already has a method named _ construct (), but it is not a constructor.
Let's look at another situation. What happens when a derived class inherits the base class and both classes have constructor and destructor?
PHP 5 handles this by hiding the construction and analysis structure of the Base class!
Test code: <? PHP /** Myclass. PHP */class baseclass {function _ construct () {print "baseclass: \ n constructor";} function _ destruct () {print "baseclass: \ n destroying ";}} class subclass extends baseclass {function _ construct () {print" subclass: \ n constructor ";} function _ destruct () {print" subclass: \ n destroying ";}}$ OBJ = new subclass () ;?>
After saving and running, the result is printed as follows: subclass: constructor subclass: destroying
We can see that the Construction and Analysis of the base class did not happen.
Why does PhP5 adopt this mechanism?
The process of constructing and destructing C ++ is: base class structure-> derived class structure-> base class structure. This is a mature mechanism. Why is PHP 5 unconventional?
Obviously, this approach is not very wise.
Looking at the PHP manual, I found a saying like this:
PHP 4 does not automatically call the base class constructor from the constructor of the derived class. It is the responsibility of the user to properly call the upper-level constructor one by one. (PHP 4)
If the constructor is defined in the subclass, the constructor of its parent class will not be called secretly. To execute the constructor of the parent class, you must call parent ::__ construct () in the constructor of the subclass (). (PHP 5)

"

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.