2016-03-04 18 views
0

我正在使用CI並嘗試使用Pthreads,並且我在控制器中有一個函數,它將調用線程,並從線程中實例化一個庫的對象,使用新的超過$這個 - >負載>我的線程繼承Thread類,因此不列入允許我做負載庫>庫,所以我用實例化新 這裏是我的控制器類:無法在pthreads中執行CI get_instance()

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 \應用程序\庫\ common_event_library.php

+0

當我做var_dump($ this - > _ CI)我得到空 –

回答

0

你Common_event_library的構造應該是

function __construct($ci_instance){ 
    $this->_CI = $ci_instance; 
} 

你傳遞她的e,但沒有收到任何結果。:

$ common_event_library = new Common_event_library($ this - > _ CI);

+0

是啊我試過了,但是當我運行命令它說CLI已停止工作,並從瀏覽器它說,該頁面不可用,你可以檢查現在我有還編輯了代碼 –

+0

此函數不存在:$ mythreads [$ i] - > start(); –

0

Common_event_library()中的構造函數必須包含 parent :: __ construct();