__construct()
是类的构造函数方法。如果你从中声明一个新的对象实例,它会运行。但是,它只运行它自己的构造函数,而不是它的父项。例如:
<?php
class A {
public function __construct() {
echo "run A's constructor\n";
}
}
class B extends A {
public function __construct() {
echo "run B's constructor\n";
}
}
// only B's constructor is invoked
// show "run B's constructor\n" only
$obj = new B();
?>
在这种情况下,如果你需要的时候被宣布$ obj转换为运行A类的构造函数,你需要使用parent::__construct()
:
<?php
class A {
public function __construct() {
echo "run A's constructor\n";
}
}
class B extends A {
public function __construct() {
parent::__construct();
echo "run B's constructor\n";
}
}
// both constructors of A and B are invoked
// 1. show "run A's constructor\n"
// 2. show "run B's constructor\n"
$obj = new B();
?>
CI中的情况下,该行运行CI_Controller中的构造函数。该构造方法应该以某种方式帮助您的控制器代码。你只是想让它为你做一切。
你b声明中缺少父扩展 – drk 2013-02-25 09:46:31
'B类扩展A'是正确的 – 2013-02-25 09:49:12
所以我们需要对那些在类似的方法新类的其他方法做同样的父类,并希望使用其父功能?或者这只是用于'__construct()'类? – 2013-02-25 09:53:49