Bug #71067 [Opn]: Local object in class method stays in memory for each call

From: Date: Wed, 09 Dec 2015 08:42:01 +0000
Subject: Bug #71067 [Opn]: Local object in class method stays in memory for each call
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197724@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71067&edit=1

 ID:                 71067
 Updated by:         laruence@php.net
 Reported by:        gal at webrats dot co dot il
 Summary:            Local object in class method stays in memory for
                     each call
 Status:             Open
 Type:               Bug
 Package:            *General Issues
 Operating System:   Centos 6.7
 PHP Version:        7.0.0
 Block user comment: N
 Private report:     N

 New Comment:

how about run php7 -n test.php


Previous Comments:
------------------------------------------------------------------------
[2015-12-09 06:13:05] 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 01:49:57] laruence@php.net

I can not reproduce this. are you meaning run your example codes from:

php7 example.php?

thanks

------------------------------------------------------------------------
[2015-12-08 22:51:46] 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
        )
)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71067&edit=1


Thread (7 messages)

« previous php.bugs (#197724) next »