php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81686 Method static variables stored across different instances
Submitted: 2021-12-01 16:56 UTC Modified: 2021-12-01 22:14 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: norrisjibril at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 8.1.0 OS: MacOs
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: norrisjibril at gmail dot com
New email:
PHP Version: OS:

 

 [2021-12-01 16:56 UTC] norrisjibril at gmail dot com
Description:
------------
When this is run on PHP8.0 and older (7.x), the static $cache variable is set to [] on each instantiation but on 8.1 it still keeps the data from the previous instantiations. 

PHP8.1 test link https://3v4l.org/qLpoT#v8.1rc3
PHP7.4 test link https://3v4l.org/qLpoT#v7.4.26

<?php

abstract class Cache{
    protected function put($key,$value){
        static $cache = [];
        var_dump($cache);
        return $cache[$key] = $value;
    }
}

$instance1 = new class extends Cache{
    
    public function store($key,$value)
    {
        return $this->put($key,$value);
    }
};

$instance1->store("one",1);
$instance1->store("two",2);
$instance1->store("three",3);


$instance2 = new class extends Cache{
    
    public function store($key,$value)
    {
        return $this->put($key,$value);
    }
};

$instance2->store("four",4);
$instance2->store("five",5);


$instance3 = new class extends Cache{
    
    public function store($key,$value)
    {
        return $this->put($key,$value);
    }
};

$instance3->store("six",6);
$instance3->store("seven",7);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-12-01 17:12 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Package: *General Issues +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2021-12-01 17:12 UTC] cmb@php.net
This is a deliberate change[1], but is not yet documented.  Since
it is already tracked in the PHP 8.1 documentation tracker[2], I'm
closing this ticket as not a bug.

[1] <https://wiki.php.net/rfc/static_variable_inheritance>
[2] <https://github.com/php/doc-en/issues/980>
 [2021-12-01 22:14 UTC] cmb@php.net
Yes, it's documented in the migration guide, but not yet in the manual proper.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC