Home  >  Article  >  php教程  >  解决PHP4.0 和 PHP5.0类构造函数的兼容问题

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

WBOY
WBOYOriginal
2016-06-13 11:42:181226browse

在 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;
?>

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