Bug #81686 [Com]: Method static variables stored across different instances

From: Date: Wed, 01 Dec 2021 20:12:27 +0000
Subject: Bug #81686 [Com]: Method static variables stored across different instances
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238103@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 Comment by: a at b dot c dot de 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: https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.core.static-variable-inheritance Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#238103) next »