On Sat, Nov 9, 2024, at 09:39, Adam Kecskes wrote:
> Hello internals,
>
> I propose adding first-class types to PHP, as this feature could greatly
> enhance the language, especially given PHP's unique runtime type-checking
> system. Many libraries currently rely on class strings as function
> parameters, which could be made significantly safer with native types rather
> than strings. This addition would not only streamline existing code but also
> create a foundation for future language extensions. For instance, generic
> classes could be implemented as syntactic sugar, which would translate into a
> class property holding the type parameter(s) for each instance.
>
> A minimal viable version could allow defining types as expressions, introduce
> a new 'type' type, and expand is_a to accept those expressions like:
>
> is_a($object, \ClassOne | \ClassTwo);
>
> An example interface might look like:
>
> interface ContainerInterface
> {
> public function get(type $type);
> }
>
> I’d appreciate your feedback on this idea before proceeding with an RFC or
> draft PR. Please let me know your thoughts!
>
> Best Regards,
> Adam Kecskes
Hey Adam,
While working on https://wiki.php.net/rfc/typed-aliases, (and an associated
early discussion: https://externals.io/message/125455#125455), it would make
pattern matching almost trivial to implement, as well as first-class types.
That being said, my life has been a bit of a whirlwind in the last month, so
I've had little time to work on these things. I'm hoping for things to settle
down soon though. 🫰
I highly recommend seeking out the pattern matching RFC that is still being
worked on as well: https://externals.io/message/123701#123701
Long story short, there is ongoing work towards what you are proposing here,
but feel free to work on competing a competing RFC or contributing to ongoing
discussions. Different ideas and approaches are almost always good, IMHO.
— Rob