Assigning array to Threaded object results in Volatile object (despite array cast)
LuckyFellow opened this issue · 1 comments
LuckyFellow commented
Environment
- PHP: 7.2.13
- pthreads: 3.2.0
- OS: linux, docker, debian stretch
Summary
Trying to assign an array to a Threaded object inside of a Threaded object ($this->store["test"][]), results in automatic creation of a Volatile object. The volatile object gets destroyed, as expected, when leaving the thread context.
Is this expected behaviour?
How must I apply type casting to produce the expected output / What do I need to do differently?
Can I expect my workaround to function properly?
Reproducing Code
<?php
$store = new Threaded();
$store["test"] = new Threaded();
$thread = new class($store) extends Thread {
public $store;
public function __construct(Threaded $store)
{
$this->store = $store;
}
public function run()
{
$this->store["test"][] = (array)[1,2,3]; // this results in Volatile object
// Uncomment the follwing lines and the expected output is produced.
// $tmp = $this->store["test"];
// $tmp[] = (array)[1,2,3];
}
};
$thread->start() && $thread->join();
var_dump($store);
?>
Expected Output
object(Threaded)#1 (1) {
["test"]=>
object(Threaded)#2 (1) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
Actual Output
object(Threaded)#1 (1) {
["test"]=>
object(Threaded)#2 (0) {
}
}
PHP Fatal error: Uncaught RuntimeException: pthreads detected an attempt to connect to an object which has already been destroyed in /app/test.php:21
Stack trace:
#0 /app/test.php(21): var_dump(Object(Threaded))
#1 {main}
thrown in /app/test.php on line 21
dktapps commented
on a side note, this casting behaviour is a serious pest and I'd love to see it gone... @sirsnyder what do you think of scrapping this and adding something like Threaded::fromArray()
/ Volatile::fromArray()
instead?