2016-09-23 88 views
1
class Test extends thread { 
    function __construct(&$db,$userObj) { 
    $this -> userObj = $userObj; 
    print "Original:"; 
    var_dump($db); 
    $this->db = $db; 
    print "InThread:"; 
    var_dump($this->db);  
    // as value of $this->db and db(in constructor) is different I am gettting different values. 
    } 
public function run(){ 
    $userId = $this->userObj->getUserId(); 
    $data = $this->db->getData(); 
// as value of $this->db and db(in constructor) is different I am getting different values. 
    } 

function getData(&$db,$userObj){ 
    $thread = new Test($db,$userObj); 
    $thread->start(); 
    } 

我想在我的運行函數中使用db的值。如何通過run()訪問線程構造函數變量而不更改$ db值。如何在運行函數中訪問線程構造函數的變量?

回答

0

將對象設置爲Threaded的成員屬性本身不是Threaded的對象將在寫入時自動序列化,並在讀取時自動進行非序列化。

當訪問Threaded成員屬性時,pthreads需要禁止返回由其他Threads創建的對象,因爲PHP的體系結構(無共享)。

如果屬性本身是Threaded那麼這是有效的管理(在PHP7中),但是你仍然沒有得到相同的物理對象。

這就是$this->db$db是不同對象的原因。

試圖按引用傳遞不會有任何區別; Threaded對象不支持對成員屬性的引用。