Numeric/string array index
PHP: 7.0.2 Pthreads: 3.1.5 OS: Ubuntu
Took me a while to find this problem in my code. From this test/example to show the problem, it looks bad that I am using a numeric and a string, but in my actual app when the array was created it was using a getnextseq() function that returned a numeric, but later on I was receiving the seq via a socket buffer to read/unset, and then it was a string.
Since in PHP numerics and strings are loosely typed, they are treated the same, so this took a bit of investigating, as I didnt spot it.
Example:
class sharedArray extends Threaded {
public function run(){}
}
class socketThread extends Thread
{
public function __construct($sharedArray)
{
$this->sharedArray = $sharedArray;
}
public function run()
{
$idx = 1;
$this->sharedArray[$idx] = 'data';
var_dump($this->sharedArray);
$idx = '1';
unset($this->sharedArray[$idx]);
var_dump($this->sharedArray);
}
}
$sharedArray = new sharedArray();
$socketThreads = new socketThread($sharedArray);
$socketThreads->start();
$socketThreads->join();
The 'unset' doesnt work, since I'm guessing because internally it looks different to pthreads.
However, this would work in standard PHP:
$idx = 1;
$array[$idx] = 'data1';
$idx = '1';
unset($array[$idx]);
var_dump($array);
Not sure if you can do something about that internally or not, or if its something I/people need to be aware of.
For now, I can just cast the return result of my getNextSeq() to a string, so its uniform in my app.
Well, implicit cast for object and arrays are a known issue/feature (depends on how you see it) in PHP. Pthreads extension definitely implement its own set/get handlers with pthreads object. About if it intentionally doesn't cast index in order to copy PHP's behavior, that is to Joe to say. However I agree with you, it must be fixed or known.
This has pretty low urgency ... it's not being ignored ;)
Added in f55ffa9250f3856c5b542f074b45cd56c5d2db82