Anonymous classes are syntax sugar that may appear deceiving to some.
The 'anonymous' class is still parsed into the global scope, where it is auto assigned a name, and every time the class is needed, that global class definition is used. Example to illustrate....
The anonymous class version...
<?php
function return_anon(){
return new class{
public static $str="foo";
};
}
$test=return_anon();
echo $test::$str; $another=get_class($test); echo $another::$str; ?>
The above is functionally the same as doing this....
<?php
class I_named_this_one{
public static $str="foo";
}
function return_not_anon(){
return 'I_named_this_one';
}
$clzz=return_not_anon();echo $clzz::$str;
?>