Codeigniter 中的构造函数
Constructor in Codeigniter
我有一个控制器 constructori
,它包含一个构造函数和一个方法 aas
。
我创建了一个新对象 $col = new constructori()
,然后我调用了该方法
echo $col->aas();
Class Constructori {
function index(){
}
public function __construct(){
echo" something <br />";
}
function aas(){
echo 'another something <br>';
}
}
$col = new Constructori();
echo $col->aas();
任何人都可以解释为什么我得到:
something
another something
something
而不是
something
something
another something
预期输出。它首先执行:
$col = new Constructori();//something
echo $col->aas();//another something
//Now codeigniter itself try to create new controller ---thats why you got something
原因是 Codeigniter 首先加载所有 class。然后创建其必要的 classes 对象。因此,当它加载 class Constructori
时,您的代码首先执行。最后 Codeigniter 自己创建了 Constructori
对象。
假设你的代码是这样的:
$col = new Constructori();
echo $col->aas();
$col2 = new Constructori();
echo $col2->aas();
输出将是:
something //for $col construct
another something //for $col->aas();
something //for $col2 construct
another something // $col2->aas();
something //last Codeigniter creates one
我有一个控制器 constructori
,它包含一个构造函数和一个方法 aas
。
我创建了一个新对象 $col = new constructori()
,然后我调用了该方法
echo $col->aas();
Class Constructori {
function index(){
}
public function __construct(){
echo" something <br />";
}
function aas(){
echo 'another something <br>';
}
}
$col = new Constructori();
echo $col->aas();
任何人都可以解释为什么我得到:
something
another something
something
而不是
something
something
another something
预期输出。它首先执行:
$col = new Constructori();//something
echo $col->aas();//another something
//Now codeigniter itself try to create new controller ---thats why you got something
原因是 Codeigniter 首先加载所有 class。然后创建其必要的 classes 对象。因此,当它加载 class Constructori
时,您的代码首先执行。最后 Codeigniter 自己创建了 Constructori
对象。
假设你的代码是这样的:
$col = new Constructori();
echo $col->aas();
$col2 = new Constructori();
echo $col2->aas();
输出将是:
something //for $col construct
another something //for $col->aas();
something //for $col2 construct
another something // $col2->aas();
something //last Codeigniter creates one