Plugin for the XP Compiler which adds an is
operator to the PHP language.
A mix of functions, syntax and XP core functionality is()
:
is_string($value) // for primitives, use is_[T]()
is_callable($value) // for pseudo types callable, array, object
is_array($value) || $value instanceof \Traversable // no is_iterable in PHP 5 and 7.0
$value instanceof Date // for value types
null === $value || is_int($value) // nullable types cannot be tested directly
is('[:string]', $value) // for types beyond PHP type system
is('string|util.URI', $value) // for types beyond PHP type system
Anything that works as a parameter, property or return type can be used with the is
operator.
$value is string
$value is callable
$value is iterable
$value is Date
$value is ?int
$value is array<string, string>
$value is string|URI
After installing the XP Compiler into your project, also include this plugin.
$ composer require xp-framework/compiler
# ...
$ composer require xp-lang/php-is-operator
# ...
No further action is required.
- https://wiki.php.net/rfc/pattern-matching
- https://docs.hhvm.com/hack/expressions-and-operators/is
- https://kotlinlang.org/docs/reference/typecasts.html
- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is
- https://externals.io/message/120655 (Proposal to introduce is operator)