PHP 4.X vs. 5.x version constructors differ from class inheritance

Source: Internet
Author: User

Today see Ecshop source code when found that the constructor is the same as the class name, have not touched before, suddenly puzzled

hp4.x version:

the constructor name for PHP 4.x is the same as the class name .
Note: The constructor of the parent class in the subclass does not execute automatically .
To execute the constructor of the parent class in a subclass, you must execute a statement similar to the following:
$this->[constructor name for parent class ()]

For example:

Class Base1
{
function Base1 ()
{
Echo ' This is base1 construct ';
}
}

class Class1 extends Base1
{
function Class1 ()
{
$this->base1 ();
Echo ' This is Class1 construct ';
}
}
$c 1 = new Class1;

php5.x version:

PHP5.0 The above version has greatly expanded the function of the class. The constructor of the class is uniformly named __construct ().
The constructor of the parent class in the subclass will not execute in two cases:
1, if the subclass does not define the constructor __construct (), the constructor of the parent class is inherited by default and is automatically executed.
2, such as the subclass defines the constructor __construct (), because the constructor name is also __construct (), so the constructor of the subclass is actually overriding (override) The constructor of the parent class. The constructor for the subclass is executed.
At this point, if you want to execute the constructor of the parent class in the subclass, you must execute a statement similar to the following:

Parent::__construct ();

For example:

Class Base2
{
function __construct ()
{
Echo ' This is BASE2 construct ';
}
function __destruct ()
{
}
}

Class Class2 extends Base2
{
function __construct ()
{
parent::__construct ();
Echo ' This is Class2 construct ';
}
}

Note Parent::__construct (); The statement must not necessarily be placed in the constructor of the subclass. Placing in the constructor of a subclass only guarantees that it executes automatically when subclasses are instantiated.

compatibility issues for PHP4.0 and class 5.0 constructors:

In PHP5.0 and above, it is also compatible with the definition rules for the 4.0 version of the constructor. If a 4.0 constructor and __construct () function are defined at the same time, the __construct () function takes precedence.
To make the class code compatible with both PHP4.0 and 5.0, you can do this in the following ways:

Class Class3
{
function __construct ()//for PHP5.0
{
Echo ' This is Class2 construct ';
}

function Class3 ()//for PHP4.0
{
$this->__construct ();
}
}

$c 3 = new Class3;

PHP 4.X and 5.x version constructors differ from class inheritance

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.