I'm running php behind nginx with php-fpm and cron tasks to php binary (/usr/bin/php).
I've found an inconsistency - the same script outputs different results when I run it through php binary and through fpm.
NOTE This only applies to PHP7. On another server I've tested it with 5.6 and the result is identical.
Here's what I've found. The following script:
<?php
class Test {
public function test(){
$arr = (object) [
'children' => []
];
$arr->children[] = 1;
return $arr;
}
}
$o = new Test();
$o->test();
print_r( $o->test() );
Saved to test.php
. When I run it through browser (php-fpm), will produce:
stdClass Object
(
[children] => Array
(
[0] => 1
)
)
But when I execute it from CLI, the result is different:
[root@server1 web]# php -f test.php
stdClass Object
(
[children] => Array
(
[0] => 1
[1] => 1
)
)
It does not happen without the (object) casting. Also if I'll instantiate $arr
with new stdClass()
it will not happen.
Seems like the $arr = (object)
is being preserved in the memory by php7's engine.
Maybe it's a configuration issue. Anyone ran into it before or can explain?
Thanks.