Home > Backend Development > PHP Tutorial > 解决PHP4.0 和 PHP5.0类构造函数的兼容问题_PHP

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

WBOY
Release: 2016-06-01 12:04:09
Original
813 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;
?>

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template