我正在研究CI并尝试使用Pthreads,我在控制器中有一个函数,它将调用线程,并且我已经使用new而不是$ this-> load->来线程化了库的对象。库作为我的线程扩展Thread类,所以它不允许我做load->库所以我使用new实例化了类 这是我的控制器:
defined('BASEPATH') OR exit('No direct script access allowed');
include_once APPPATH.'/libraries/feedstart_class.php';
include_once APPPATH.'/libraries/test/thread.php';
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('event_library');
$this->load->model('global_model');
}
public function testThreadCommandLine() {
$mythreads = array();
$ci_obj = & get_instance();
for($i = 0;$i<5;$i++) {
$mythreads[$i] = new MyThread();
$mythreads[$i]->setIndex($i);
$mythreads[$i]->setCI($ci_obj);
$mythreads[$i]->start();
}`enter code here`
}
}
这是我的主题类
include_once APPPATH.'/libraries/common_event_library.php';
Class MyThread extends Thread
{
private $_index;
private $_CI;
public function __construct() {
}
public function setIndex($index) {
$this->_index = $index;
}
public function setCI($ci_obj) {
$this->_CI = $ci_obj;
}
public function getIndex() {
return $this->_index;
}
public function run() {
sleep(rand(0,10));
echo "The index of the object is ".$this->_index;
$common_event_library = new Common_event_library($this->_CI);
$common_event_library->display();//gets all sports categories from our system
echo "<br>";
}
}
这是我的库
Class Common_event_library {
private $_CI;
public function __construct($ci_instance) {
$this->_CI = $ci_instance;
}
public function display() {
$this->_CI->db->select("*");
$this->_CI->db->from("sports_cateogry");
$result = $this->_CI->db->get()->result();
print_r($result);
}
}
当我从浏览器和命令行运行testThreadCommandLine时,它会说:注意:尝试在C:\ xampp \ htdocs \ CI \ application \ libraries \ common_event_library.php中获取非对象的属性
答案 0 :(得分:0)
Common_event_library的构造函数应该是
function __construct($ci_instance){
$this->_CI = $ci_instance;
}
你在这里传递它,但没有任何东西接收它。:
$ common_event_library = new Common_event_library($ this-&gt; _CI);
答案 1 :(得分:0)
Common_event_library()中的构造函数必须包含 父:: __构建体();