caincheung 发表于 2008-4-14 23:27:30

为什么新版本得这样写?

<?php
class Index extends Controller {
function __construct() {
parent::Controller();
}
function Index() {
echo 'Hello World';
}
}
?>
不定义__construct就会Show出两个Hello World。。?????

[ 本帖最后由 caincheung 于 2008-4-15 08:16 编辑 ]

Hex 发表于 2008-4-14 23:37:39

我给你编辑了,自己看代码~

gwpxjb 发表于 2008-4-15 11:59:31

那样写相当于2构造函数了吧?

你类名是Index,,类里边有定义了index方法(相当于构造函数~~)
里边你还申请了__construct()构造函数

gwpxjb 发表于 2008-4-15 16:28:04

php5以前的版本,构造函数的申明都是与类名相同
如:

<?php
class Index extends Controller{
   //构造函数
    function index(){
      parent::Controller();
    }
}

而在php5中,构造函数是可以这样写的

function __construct(){

}

Hex 发表于 2008-4-15 17:18:20

对,楼主那个是产生了PHP5和4的兼容性问题了,不能同时使用PHP4和5的构造函数写法。
页: [1]
查看完整版本: 为什么新版本得这样写?