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();
  }

I want to use the value of db in my run function. How to access thread constructor variable via run() without changing $db values.

Anubhav Singh
  • 432
  • 1
  • 5
  • 15

1 Answers1

0

Objects set as member properties of Threaded objects that are not themselves Threaded will be automatically serialized on write, and automatically unserialized on read.

When a Threaded member property is accessed, pthreads needs to prohibit the return of objects created by other Threads because of the architecture of PHP (share nothing).

If the property itself is Threaded then this is efficiently managed (in PHP7), but you still do not get the same physical object.

This is the reason that $this->db and $db are different objects.

Trying to pass by reference won't make any difference; Threaded objects do not support references to member properties.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62