2016-09-23 89 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对象不支持对成员属性的引用。