Home >Backend Development >PHP Tutorial >Solve the compatibility problem of PHP4.0 and PHP5.0 class constructors_PHP Tutorial

Solve the compatibility problem of PHP4.0 and PHP5.0 class constructors_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:05733browse

In PHP5.0 and above, it is also compatible with the constructor definition rules of version 4.0. If both the 4.0 constructor and the __construct() function are defined, the __construct() function takes precedence.
In order to make the class code compatible with PHP 4.0 and 5.0 at the same time, you can take the following method:

Copy the code The code is as follows:

class MyClass {
function __construct() { //for PHP5.0
echo 'this is class2 construct';
}
// In order to make the class The code is compatible with both PHP4.0 and 5.0
function MyClass() { //for PHP4.0
$this->__construct();
}
}
$c3 = new MyClass ;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328098.htmlTechArticleIn PHP5.0 and above, it is also compatible with the constructor definition rules of version 4.0. If both the 4.0 constructor and the __construct() function are defined, the __construct() function takes precedence. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn