From http://www.ximenyifan.com
"
In PhP4, the constructor of a class is a function with the same name by default.
PhP5 not only follows this naming method, but also introduces some new elements:
_ Construct () is a method name, which is the default constructor in PhP5
What are the advantages of this function? First, when you change the class name, its constructor no longer needs to change with the class name.
Second
- Class onE {
- Function onE {
- $ This-> test ();
- }
- }
- Class Two extends onE {
- Function test {
- Echo & quot; 123 & quot ";
- }
- }
- $ Test = new two ();
CopyCode
This is an incorrect PHP 4 syntax.Class E is inherited and executed simultaneously onThe construction of E is still, the test method is called at this time, but at this time the test is not onE.
- Class onE {
- Function _ construct (){
- $ This-> test ();
- }
- }
- Class Two extends onE {
- Function test {
- Echo & quot; 123 & quot ";
- }
- }
- $ Test = new two ();
Copy code
In PhP5, this problem was solved through _ construct.
At the same time, parent ::__ construct () distinguishes between the constructor of the parent class and the Child class.
PhP5 not only has _ construct () but also has _ destruct ()
We call it destructor:
The constructor is opposite to the constructor. The Destructor is newly added to PhP5 and does not contain the destructor in PhP4. The Destructor allows you to perform operations or complete some functions before destroying a class, such as closing a file and releasing a result set, the Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the Destructor is called before the object is destroyed in the memory. Similar to the constructor name, The Destructor name of a class must be _ destruct ()
PHP also has interfaces. interfaces are defined as interface classes.
The PHP method also distinguishes private public and so on. Generally, the default value is public.
"