When refactoring arrays into object, you can count on #PHP 's dynamic syntax to keep all your code working while replacing Plain Old Arrays by classes.
Learn how to harness PHP dual syntax array-objects!
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.
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.
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 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.
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.
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.
@merms I like to take into account the types for that kind of problem : keys are of type string|int. When they are used with a string-only or int-only method (trim() here), a cast is necessary.
I have to say this is quite skewed towards string types, as PHP never juggles integers to strings.
Some 'integer strings' stay integers, though. 14 digits and longer, for example.
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