Php object-oriented (OOP)-inheritance and compatibility of constructor methods of different php versions

Source: Internet
Author: User
: This article mainly introduces the inheritance and compatibility of php object-oriented (OOP)-constructor methods of different php versions. For more information about PHP tutorials, see. PHP4.x:

The PHP 4.x constructor name is the same as the class name.

The constructor name of the subclass is the same as that of the subclass ).

The constructor of the parent class in the subclass is not automatically executed.

To execute the constructor of the parent class in the subclass, you must execute statements similar to the following:

$ This-> [constructor name of the parent class ()]

Class base1 {
Function base1 (){
Echo 'this is base1 construct ';
}
}
Class class1 extends base1 {
Function class1 (){
$ This-> base1 ();
Echo 'this is class1 construct ';
}
}
$ C1 = new class1;

PHP5.x:

PHP5.0 and later versions greatly expand the functions of the class. Class constructor is named _ construct ().

The construct name of the subclass is also _ construct () (also nonsense ).

Will the constructor of the parent class be executed in the subclass? there are two situations:

1. if the subclass does not define construct_construct (), the constructor of the parent class will be inherited by default and automatically executed.

2. if the subclass defines the constructor _ construct (), because the constructor name is also _ construct (), the constructor of the subclass actually overwrites (override) the constructor of the parent class. At this time, the constructor of this subclass is executed.

To execute the constructor of the parent class in the subclass, you must execute a statement similar to the following:

Parent: :__ construct ();

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 that parent ::__ construct (); statements do not have to be placed in the constructor of the subclass. The constructor in the subclass only ensures that it is automatically executed when the subclass is instantiated.

Compatibility of PHP4.0 and 5.0 class constructor:

PHP5.0 and later versions are compatible with the definition rules of constructor 4.0. If both the constructor 4.0 and the _ construct () function are defined, the _ construct () function takes precedence.

To make the class code compatible with both PHP4.0 and 5.0, you can use the following methods:

Class class3 {
Function _ construct () // for PHP5.0
{
Echo 'this is class2 construct ';
}
Function class3 () // for PHP4.0
{
$ This-> _ construct ();
}
}
$ C3 = new class3;

The above content is derived from "fried peanuts". thank you for sharing it.

The above introduces the inheritance and compatibility of php object-oriented (OOP)-constructor methods of different php versions, including some content, and hope to help those who are interested in PHP tutorials.

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.