On Tue, May 25, 2021 at 4:20 AM Karoly Negyesi <kar...@negyesi.net> wrote: > I was wondering whether $array->map($somefunction) would be possible. I am > not a C programmer by any stretch but reading ZEND_VM_HOT_OBJ_HANDLER(112 > it seems to me it should be quite easy (famous last words) to find out if > object is an array and if so then > > 1. prepend the string array_ before the method name > 2. based on a small lookup table move the "object" to the right place -- > either first argument or second. > 3. do a function call instead of a method call. >
While I don't love the specifics of the proposal, I am 100% in favor of allowing arrays to be used in an object-like fashion. What I don't like about the specific proposal is that it's just a little too magic in its function selection and argument mapping. There's also the fact that it doesn't leave room to improve specifics about the implementations of the methods. I'd much rather seen an `Array` class defined with specific methods declared on it. In many cases these will be simple trampolines to an existing function, but it gives us self-documenting stubs and room to wiggle out of poor decisions from the 1990s. Such a class would not be instantiable or inheritable, it would just exist as a lightweight ValueObject for performing the method invocations (we can make it internally instantiable using tricks like not calling a private constructor). Then some hand-wavey details about maybe returning objects which have a cast-to-array handler, mumble mumble, devil in the details... waving hands... -Sara