note 123605 added to language.oop5.anonymous

From: Date: Tue, 12 Feb 2019 12:14:57 +0000
Subject: note 123605 added to language.oop5.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-210232@lists.php.net to get a copy of this message
I wanted to share my findings on static properties of anonymous classes. So, given an anonymous class' object generating function like this: <?php function nc () { return new class { public static $prop = []; }; } ?> Getting a new object and changing the static property: <?php $a = nc(); $a::$prop[] = 'a'; var_dump($a::$prop); // array(1) { // [0] => // string(1) "a" // } ?> Now getting another object and changing the static property will change the original one, meaning that the static property is truly static: <?php $b = nc(); $b::$prop[] = 'b'; var_dump($b::$prop); // Same as var_dump($a::$prop); // array(2) { // [0] => // string(1) "a" // [1] => // string(1) "b" // } assert($a::$prop === $b::$prop); // true ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.142 (proxied: 148.63.55.13) ---- Manual Page -- http://php.net/manual/en/language.oop5.anonymous.php Edit -- https://master.php.net/note/edit/123605 Del: integrated -- https://master.php.net/note/delete/123605/integrated Del: useless -- https://master.php.net/note/delete/123605/useless Del: bad code -- https://master.php.net/note/delete/123605/bad+code Del: spam -- https://master.php.net/note/delete/123605/spam Del: non-english -- https://master.php.net/note/delete/123605/non-english Del: in docs -- https://master.php.net/note/delete/123605/in+docs Del: other reasons-- https://master.php.net/note/delete/123605 Reject -- https://master.php.net/note/reject/123605 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#210232) next »