我有一个CodeIgniter项目,我想通过CLI调用我的一个控制器方法,但是正常属性&附加到CI超级对象的方法似乎缺失了?
例如,运行以下脚本在正常的http请求期间运行完全正常会产生错误:
class Worker extends MY_Controller {
public function __construct() {
if(php_sapi_name() !== 'cli') {
show_404();
}
}
public function test(){
$this->load->library('some_library');
}
}
这是通过CLI返回的错误
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: controllers/worker.php</p>
<p>Line Number: 21</p>
</div>PHP Fatal error: Call to a member function library() on a non-object in /Users/casey/Documents/workspaces/vibecompass_live/application/controllers/worker.php on line 21
Fatal error: Call to a member function library() on a non-object in /Users/casey/Documents/workspaces/vibecompass_live/application/controllers/worker.php on line 21
我正在调用脚本: $ php index.php worker test
修改
此外,这个脚本:
class Worker extends MY_Controller {
public function __construct() {
if(php_sapi_name() !== 'cli') {
show_404();
}
}
public function test(){
$CI =& get_instance();
var_dump($CI); die();
$this->load->library('some_library');
}
}
返回:NULL
答案 0 :(得分:2)
看起来你没有初始化父类,在你的控制器构造函数中,也调用父构造函数:
class Worker extends MY_Controller {
public function __construct() {
parent::__construct();
if(php_sapi_name() !== 'cli') {
show_404();
}
}