php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71067 Local object in class method stays in memory for each call
Submitted: 2015-12-08 22:51 UTC Modified: 2015-12-09 12:47 UTC
From: gal at webrats dot co dot il Assigned: laruence
Status: Closed Package: *General Issues
PHP Version: 7.0.0 OS: Centos 6.7
Private report: No CVE-ID:
 [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
        )
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-09 01:49 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2015-12-09 01:49 UTC] laruence@php.net
I can not reproduce this. are you meaning run your example codes from:

php7 example.php?

thanks
 [2015-12-09 06:13 UTC] gal at webrats dot co dot il
-Status: Feedback +Status: Open
 [2015-12-09 06:13 UTC] gal at webrats dot co dot il
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
        )
)
 [2015-12-09 08:41 UTC] laruence@php.net
how about run php7 -n test.php
 [2015-12-09 08:47 UTC] gal at webrats dot co dot il
Same result:

[root@server web]# php -n test.php
stdClass Object
(
    [children] => Array
        (
            [0] => 1
            [1] => 1
        )
)
 [2015-12-09 12:33 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2015-12-09 12:47 UTC] laruence@php.net
I can reproduce it now. thanks
 [2015-12-09 13:09 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4c379392b2873cf2d5407cade3f747401f7df96c
Log: Fixed bug #71067 (Local object in class method stays in memory for each call)
 [2015-12-09 13:09 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4c379392b2873cf2d5407cade3f747401f7df96c
Log: Fixed bug #71067 (Local object in class method stays in memory for each call)
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Feb 19 11:01:37 2017 UTC