update page now

Voting

: max(five, six)?
(Example: nine)

The Note You're Voting On

sebastian.wasser at gmail
7 years ago
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
?>

<< Back to user notes page

To Top