php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73872 Static method variable state depends on file include
Submitted: 2017-01-05 13:34 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sc at shpock dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: 7.0.14 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sc at shpock dot com
New email:
PHP Version: OS:

 

 [2017-01-05 13:34 UTC] sc at shpock dot com
Description:
------------
Setup: a parent class which defines a static variable in a method. The static variable is initialized with NULL and set to some value on the first method call. Then, a child class calls the same (inherited) method.

If the child class definition is in the same file as the parent class definition, this works as expected. However, if the child class definition is loaded from a separate file and this file is loaded after(!) the first call to the method which sets the static variable, the static variable seems to be "shared" between both classes.

I'm really curious about an explanation in case this is not a bug because i would like to understand the concept. Thank you!

Test script:
---------------
class ParentClass
{
    protected $something = 'parent';
    public function getSomething()
    {
        static $cache;
        return $cache !== null
            ? $cache
            : $cache = $this->something;
    }
}


$parent = new ParentClass();
var_dump($parent->getSomething()); // expected: 'parent'; returned: 'parent'

include 'child.php';
# content of child.php:
//class ChildClass extends ParentClass
//{
//    protected $something = 'child';
//}

$child = new ChildClass();
var_dump($child->getSomething()); // expected 'child'; returned 'parent'


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Nov 17 12:01:34 2019 UTC