Re: [RFC] Pattern Matching

From: Date: Tue, 02 Dec 2025 20:00:32 +0000
Subject: Re: [RFC] Pattern Matching
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-129513@lists.php.net to get a copy of this message
On Tue, Dec 2, 2025, at 12:26 AM, Dmitry Derepko wrote: > Thank you for the RFC and that you included option with match and > individual IS’s, I think it would be preferable option not to break the > match construction. > > Regarding the patterns, is there a way to declare and reuse the > patterns? Assign it to a variable and use it as “is $pattern” or > something like that. Maybe as a separate class “$pattern->match($var)”. > While it’s not reusable it looks like it has a limited scope to use it, > otherwise regular replacements preferred. Not currently, no. It's something we've discussed, but it's also kinda hard, and I don't believe any other languages do it. What you could do is just wrap it in a short closure. $pattern = fn($x) => $x is some|Pattern|null; $pattern($something); I'd love to have a way to shorten that, but so far we haven't come up with one. Suggestions welcome, as I'd love to be able to do something like this (obviously not exactly this): $filtered = array_filter($arr, is Point(x: <10)); --Larry Garfield

« previous php.internals (#129513) next »