Bug #81686 [Nab]: Method static variables stored across different instances
| From: | cmb@php.net | Date: | Wed, 01 Dec 2021 22:14:36 +0000 |
| Subject: | Bug #81686 [Nab]: Method static variables stored across different instances | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-238107@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81686&edit=1
ID: 81686
Updated by: cmb@php.net
Reported by: norrisjibril at gmail dot com
Summary: Method static variables stored across different
instances
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: MacOs
PHP Version: 8.1.0
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Yes, it's documented in the migration guide, but not yet in the manual proper.
Previous Comments:
------------------------------------------------------------------------
[2021-12-01 20:12:27] a at b dot c dot de
https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.core.static-variable-inheritance
------------------------------------------------------------------------
[2021-12-01 17:12:06] 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 16:56:51] 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);
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81686&edit=1