# [Proposal] Introduce is operator

- Thread: https://externals.io/message/120655
- Original archive: https://news-web.php.net/php.internals/120655
- Started by udaltsov.valentin@gmail.com on 2023-06-21T22:44:25+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #120655 — udaltsov.valentin@gmail.com — 2023-06-21T22:44:25+00:00

URL: https://externals.io/message/120655#120655

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

---

## #120656 — Larry Garfield — 2023-06-22T03:08:00+00:00

URL: https://externals.io/message/120655#120656 · In reply to: #120655

> 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

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
