I have a solution but not a solid explanation, so more answers would be very welcome.
Here is my Threaded
child (trimmed for the sake of brevity):
class ObjectConstructorThreaded extends Threaded
{
protected $worker;
protected $className;
protected $parameters;
protected $objectKey;
public function __construct($className, $parameters)
{
$this->className = $className;
$this->parameters = $parameters;
}
public function setWorker(\Worker $worker)
{
$this->worker = $worker;
}
protected function getWorker()
{
return $this->worker;
}
public function run()
{
$reflection = new \ReflectionClass($this->className);
$instance = $reflection->newInstanceArgs($this->parameters);
$this->objectKey = $this->getWorker()->notifyObject($instance);
}
public function getObjectKey()
{
return $this->objectKey;
}
}
And the Worker
(again trimmed):
class ObjectServer extends Worker
{
protected $count = 0;
protected $objects = array();
public function notifyObject($object)
{
$key = $this->generateHandle();
/*
// Weird, this does not add anything to the stack
$this->objects[$key] = $object;
// Try pushing - fail!
$this->objects[] = $object;
// This works fine? (but not very useful)
$this->objects = array($key => $object);
*/
// Try adding - also fine!
$this->objects = $this->objects + array($key => $object);
return $key;
}
}
Finally, to kick off the thread:
$thread = new ObjectServer();
$thread->start();
$threaded = new ObjectConstructorThreaded($className, $parameters);
$threaded->setWorker($this->worker);
$thread->stack($threaded);
As you can see from the unadulterated comments I wrote at the time, attempts to insert or push to the array failed, but rewriting it (by setting it to a fixed value or a merge of the old value and the new entry) seems to work.
I'm therefore regarding threading as making non-trivial types (arrays and objects) effectively immutable, and that they can only be reset and not modified. I've had the same experience with serialisable classes too.
As to why this is the case, or if there is a better approach, I will update this answer if I find out!