Comparison of memory usage and execution speed between #PHP associative arrays, named and anonymous classes, stdClasses and extended classes, across PHP versions.
TLDR; using classes is more memory efficient, and a bit faster.
The #PHP (array) operator is a simple cast to array. Yet, it is one of the less obvious feature of the language : (array) on integers, on null, on objects, recursive and hidden.
That's a lot to discover for the oldest and most useful operator.
To make a class alias, use class_alias();
To make an interface alias, use class_alias();
To make an enum alias, use class_alias();
To make a trait alias, use class_alias();
#PHP foreach() works on arrays and objects. On objects, it skips uninitialized properties, which may be very convenient with readonly properties : otherwise, it is a fatal error!
A quick review of what has changed in switch() with #PHP 8.
switch() is impacted by the integer / string comparison evolution, and also by special tricks with null. All these are detailled in this article. Lots of #phptip#phptrick
In the list below, there is one #PHP fatal error. All others are working as expected, except for the fact that they probably should yield an error and be not legit.
#PHP yield keyword allows anything as key, and that is transmitted to the calling foreach() command. So, you can have arrays or closures as keys, or worse.
There are three methods to ensure that this little cleanup code is always executed, no matter what : register_shutdown_function(), function __destruct() and try-finally.
It is possible to put 2 elements in a #PHP array, find different 5 keys with array_key_exists or isset) and yet, still count 2 distinct elements (key wise).
The type-juggling for array keys is applied in every #PHP features, to keep things easy to use.
This code is one rare way to show how it still leaks. Depending on the context, it might be very confusing.