用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3678|回复: 4
收起左侧

为什么新版本得这样写?

[复制链接]
发表于 2008-4-14 23:27:30 | 显示全部楼层 |阅读模式
PHP复制代码
<?php
class Index extends Controller {
function __construct() {
  parent::Controller();
}
function Index() {
  echo 'Hello World';
}
}
?>
复制代码

不定义__construct就会Show出两个Hello World。。?????

[ 本帖最后由 caincheung 于 2008-4-15 08:16 编辑 ]
发表于 2008-4-14 23:37:39 | 显示全部楼层
我给你编辑了,自己看代码~
发表于 2008-4-15 11:59:31 | 显示全部楼层
那样写相当于2构造函数了吧?

你类名是Index,,类里边有定义了index方法(相当于构造函数~~)
里边你还申请了__construct()构造函数
发表于 2008-4-15 16:28:04 | 显示全部楼层
php5以前的版本,构造函数的申明都是与类名相同
如:
PHP复制代码
 
<?php
class Index extends Controller{
   //构造函数
    function index(){
        parent::Controller();
    }
}
 
复制代码

而在php5中,构造函数是可以这样写的
PHP复制代码
 
function __construct(){
 
}
 
复制代码
发表于 2008-4-15 17:18:20 | 显示全部楼层
对,楼主那个是产生了PHP5和4的兼容性问题了,不能同时使用PHP4和5的构造函数写法。

本版积分规则