nikic / scalar_objects

Extension that adds support for method calls on primitive types in PHP
MIT License
1.13k stars 44 forks source link

A proposal: Maybe separating numeric and associative array handler registration? #21

Closed HallofFamer closed 9 years ago

HallofFamer commented 9 years ago

Well I've been playing with this on my own WAMP server and it's fabulous, oh my goodness how I wish this makes it to PHP 7 core. But lets take a step back, I think theres still some suggestions we can make this even better before moving forward. One idea I have is that array can be handled as numeric array and associative array. My idea is that for numeric array, it will still use the standard array handler as 'array', while for associative array it will be assigned to 'assoc'. This way we can distinguish between numeric and associative array, which is way better in my eyes. So here are my example:

register_primitive_type_handler('array', 'NumericArrayHandler'); // assigns handler to numeric array register_primitive_type_handler('assoc', 'AssociativeArrayHandler'); // assigns handler to assoc array

What is your take on this? Please let me know.

nikic commented 9 years ago

PHP, at least currently, does not distinguish between numeric and associative arrays - we could only determine this based on a full array scan (which is of course slow). In any case I'm also not really convinced it makes sense to provide two different APIs for them - this would only work well if we separate them in other parts of the language as well (in particular we'd need two typehints for example).

HallofFamer commented 9 years ago

I see, I understand. So will you consider proposing this to PHP RFC if the scalar type hinting RFC get passed? I think scalar object will work very well with scalar type hinting.

nikic commented 9 years ago

I've answered in #20.