当前位置:首页 > PHP教程 > PHP高级教程

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

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

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

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!