PHP5 constructor the ____ function

Source: Internet
Author: User

The constructor appears in the PHP4, and then adds the destructor in the PHP5. This makes PHP more appealing to the object-oriented. When PHP4, the 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"; }}//to create a new instance of $obj = (). >

Save the above code as myclass.php, and after it is run, it will print out the word is MyClass. This is the PhP4 constructor (of course, in order to be compatible, PHP5 can write as well).
In php5, there are specialized constructs and destructors. __construct () and __destruct (). and rewrite this myclass.php again.         <?php/** myclass.php*/class MyClass {function __construct () {print "constructor\n";         function __destruct () {print ' destroying\n '; }}//to create a new instance of $obj = (). >
After the save found the "constructor destroying" was printed out. Explain that construction and destructor did occur.
So far, there is no problem. In the PHP5 manual, there is also a description of the use of __construct. As follows:
For backward compatibility (PHP 4), if PHP 5 does not find the __construct () function in the class, it tries to find the old constructor, which is a function with the same name as the class. So the only thing that will create a compatibility problem is that there is already a method named __construct () in the class, but it is not a constructor.
Again, what happens when a derived class inherits the base class and the two classes all have constructs and destructor functions?
The way PHP 5 handles This is to hide the construction and destructor 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&nbSP; ";        }}  $obj  = new subclass ();? >
After the save run, the results print: Subclass:constructor subclass:destroying
As you can see, the construction and destructor of the base class did not occur.
This has to be puzzling, why PHP5 will adopt this mechanism.
The process of the construction and deconstruction of C + + is: The base class constructs the-> derived class constructs-> the derivation class-> the base class destructor. This is the mature mechanism. Why PHP 5 is unconventional.
Obviously, this approach is not very sensible.
Continue to look at the PHP manual, the result found that a statement:
PHP 4 does not automatically call the constructor of the base class from the constructor of the derived class. It is the user's responsibility to call the first-level constructor at a place. (PHP 4)
If a constructor is defined in a subclass, it does not implicitly invoke the constructor of its parent class.  To execute the constructor of the parent class, you need to 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.