Lamp development essentials (1): Make PHP class constructors compatible with version 4.0 and version 5.0

Source: Internet
Author: User

PHP versions 4.0 and 5.0 differ greatly in class support. CompileCodeWe strongly recommend that you do not mix the 4.0 and 5.0 syntaxes.

However, in some cases, you may need to consider the compatibility of the two-generation version. The following code can be run simultaneously in PHP 4.0 and 5.0, with the same results, taking into account two versions.

Principle: php5.0 is backward compatible with the syntax of the 4.0 class. If _ construct () and Class Name functions appear at the same time, _ construct () is executed first. However, in php4.0, the _ construct () function is treated as a normal function and ignored.

Therefore, if the following code is executed in the php5.0 environment, the _ construct () function is executed first and the class3 () function is ignored.
If it is executed in the php4.0 environment, the class3 () function is executed first and the _ construct () function is ignored. However, the class3 () function explicitly calls _ construct () function.
Therefore, in both versions, the _ construct () function is automatically executed to implement the constructor.
If the following code uses a 4.0 unrecognized syntax, such as parent and self, it cannot achieve Upward compatibility.

<? PHP
// Enable the error message
Error_reporting (e_all | e_strict );

Class class3
{
Function _ construct () // For php5.0
{
Echo 'this is class3 construct ';
}

Function class3 () // For php4.0
{
$ This->__ construct ();
}
}

$ C3 = new class3;
?>

Author: Zhang Qing (mesh) 2010-4-1
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com

next article: lamp development essentials (2): WordPress plug-in mechanism analysis

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.