|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2015-12-08 22:51 UTC] gal at webrats dot co dot il
 Description:
------------
Local object variable inside a class method is not recreated in each function call when executed from cli.
Works properly through php-fpm but outputs a different result when executed through cli.
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.
Test script:
---------------
<?php
class Test {
    public function test(){
        $arr = (object) [
            'children' => []
        ];
        $arr->children[] = 1;
        return $arr;
    }
}
$o = new Test();
$o->test();
print_r( $o->test() );
Expected result:
----------------
stdClass Object
(
    [children] => Array
        (
            [0] => 1
        )
)
Actual result:
--------------
stdClass Object
(
    [children] => Array
        (
            [0] => 1
            [1] => 1
        )
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 01:00:01 2025 UTC | 
Saved test script to test.php. Run using php -f test.php $ root@server web]# php -v PHP 7.0.0 (cli) (built: Dec 2 2015 21:42:51) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies $ [root@server web]# php -f test.php stdClass Object ( [children] => Array ( [0] => 1 [1] => 1 ) )Same result: [root@server web]# php -n test.php stdClass Object ( [children] => Array ( [0] => 1 [1] => 1 ) )