Home > php教程 > php手册 > body text

解决PHP4.0 和 PHP5.0类构造函数的兼容问题

WBOY
Release: 2016-06-13 11:42:18
Original
1239 people have browsed it

在 PHP5.0 以上版本里,还兼容了 4.0 版本的构造函数的定义规则。如果同时定义了4.0的构造函数和 __construct()函数,则__construct() 函数优先。
为了使类代码同时兼容 PHP4.0 和 5.0,可以采取以下的方式:

复制代码 代码如下:


class MyClass {
 function __construct() { //for PHP5.0
  echo 'this is class2 construct';
 }
 // 为了使类代码同时兼容 PHP4.0 和 5.0
 function MyClass() { //for PHP4.0
  $this->__construct();
 }
}
$c3 = new MyClass;
?>

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!