php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72219 Local object in class method stays in memory for each call
Submitted: 2016-05-15 17:03 UTC Modified: 2016-07-30 12:56 UTC
From: jaswsinc at wsharks dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 7.0.6 OS: Ubuntu 14.04 LTS
Private report: No CVE-ID: None
 [2016-05-15 17:03 UTC] jaswsinc at wsharks dot com
Description:
------------
This is almost the exact same issue seen here: https://bugs.php.net/bug.php?id=71067

That bug got zapped, but I am still able to reproduce the same issue whenever I use `foreach()` by reference. The object remains cached in memory.

Test script:
---------------
<?php
class Example
{
    protected $property;

    public function __construct()
    {
        $test = (object) ['property' => 0];
        print_r($test); // Expecting `property=0`.

        foreach ($test as $_property => &$_value) {
            $this->{$_property} = &$_value;
            $this->{$_property} = 1;
        } unset($_value);
    }
}
new Example();
new Example();

Expected result:
----------------
stdClass Object
(
    [property] => 0
)
stdClass Object
(
    [property] => 0
)

Actual result:
--------------
stdClass Object
(
    [property] => 0
)
stdClass Object
(
    [property] => 1
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-15 18:38 UTC] nikic@php.net
This is probably caused by bug #71266. The object HT is not separated before iteration.
 [2016-07-30 12:56 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2016-07-30 12:56 UTC] nikic@php.net
As per previous comment, this is a duplicate of bug bug #71266, which has been fixed in PHP 7.1 (not sure if this will be backported).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC