Hi internals,
Today PHP is more typed than ever, however, one feature is still missing:
ability to check complex variable types using standard type declarations.
For example, to check that $var is a nullable string one has to do
assert($var === null || is_string($var))
. To check that $var is false or
a countable iterator, one has to write assert($var === false || ($var instanceof Countable && $var instanceof Iterator))
.
I propose to add a new "is" operator that allows to have assert($var is ?string)
and assert($var is false|(Countable&Iterator))
. So it is
similar to instanceof, but accepts any valid PHP type declaration on the
right side.
WDYT?
Regards,
Valentin
Hi internals,
Today PHP is more typed than ever, however, one feature is still missing:
ability to check complex variable types using standard type declarations.For example, to check that $var is a nullable string one has to do
assert($var === null || is_string($var))
. To check that $var is false or
a countable iterator, one has to writeassert($var === false || ($var instanceof Countable && $var instanceof Iterator))
.I propose to add a new "is" operator that allows to have
assert($var is ?string)
andassert($var is false|(Countable&Iterator))
. So it is
similar to instanceof, but accepts any valid PHP type declaration on the
right side.WDYT?
Regards,
Valentin
There's already an RFC for that in the works, from Ilija and I: https://wiki.php.net/rfc/pattern-matching
"In the works" in this case means it's been designed and some preliminary code written a while ago, but nothing submittable yet. We're hoping to get back to it in 8.4, as it's going to be a prerequisite for ADTs being usable. (cf: https://wiki.php.net/rfc/tagged_unions, though the design there is out of date.)
So... hopefully stay tuned.
--Larry Garfield