xp-lang / php-is-operator
Is operator for PHP
v2.0.0
2024-03-24 13:02 UTC
Requires
- php: >=7.4.0
- xp-framework/compiler: ^9.0 | ^8.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
Plugin for the XP Compiler which adds an is
operator to the PHP language.
Before
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
After
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
Installation
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.
See also
- 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)