PHP Type
********
.[perex]{data-version:3.2.5}
[api:Nette\Utils\Type] is a class for working with PHP data types.
Installation:
```shell
composer require nette/utils
```
All examples assume the following class alias is defined:
```php
use Nette\Utils\Type;
```
fromReflection($reflection): ?Type .[method]
--------------------------------------------
This static method creates a `Type` object based on reflection. The parameter can be a `ReflectionMethod` or `ReflectionFunction` object (returns the return value type), or a `ReflectionParameter` or `ReflectionProperty` object. It resolves `self`, `static`, and `parent` to the actual class name. If the subject has no type, it returns `null`.
```php
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
```
fromString(string $type): Type .[method]
----------------------------------------
This static method creates a `Type` object from its string representation.
```php
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
```
getNames(): (string|array)[] .[method]
--------------------------------------
Returns an array of strings representing the subtypes that make up a compound type. For intersection types, it returns an array of arrays.
```php
$type = Type::fromString('string|null'); // or '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
```
getTypes(): Type[] .[method]
----------------------------
Returns an array of `Type` objects representing the subtypes that make up a compound type:
```php
$type = Type::fromString('string|null'); // or '?string'
$type->getTypes(); // [Type::fromString('string'), Type::fromString('null')]
$type = Type::fromString('(Foo&Bar)|string');
$type->getTypes(); // [Type::fromString('Foo&Bar'), Type::fromString('string')]
$type = Type::fromString('Foo&Bar');
$type->getTypes(); // [Type::fromString('Foo'), Type::fromString('Bar')]
```
getSingleName(): ?string .[method]
----------------------------------
For simple types (including simple nullable types like `?string`), returns the type name. Otherwise, returns null.
```php
$type = Type::fromString('string|null');
echo $type; // '?string'
echo $type->getSingleName(); // 'string'
$type = Type::fromString('?Foo');
echo $type; // '?Foo'
echo $type->getSingleName(); // 'Foo'
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
echo $type->getSingleName(); // null (it's a union type)
```
isSimple(): bool .[method]{data-version:3.2.9}
----------------------------------------------
Returns whether it is a simple type. Simple types also include simple nullable types (e.g., `?string`, `?Foo`):
```php
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // or 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true (because it contains null)
```
isUnion(): bool .[method]
-------------------------
Returns whether it is a union type (contains `|`).
```php
$type = Type::fromString('Foo&Bar');
$type->isUnion(); // true
```
isIntersection(): bool .[method]
--------------------------------
Returns whether it is an intersection type (contains `&`).
```php
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
```
isBuiltin(): bool .[method]
---------------------------
Returns whether the type is simple and also a PHP built-in type (like `string`, `int`, `array`, `callable`, etc.).
```php
$type = Type::fromString('string');
$type->isBuiltin(); // true
$type = Type::fromString('string|int');
$type->isBuiltin(); // false
$type = Type::fromString('Foo');
$type->isBuiltin(); // false
```
isClass(): bool .[method]
-------------------------
Returns whether the type is simple and also a class name (not a built-in type like `string` or `int`).
```php
$type = Type::fromString('string');
$type->isClass(); // false
$type = Type::fromString('Foo|null');
$type->isClass(); // true
$type = Type::fromString('Foo|Bar');
$type->isClass(); // false
```
isClassKeyword(): bool .[method]{data-version:3.2.6}
----------------------------------------------------
Returns whether the type is one of the internal keywords `self`, `parent`, or `static`.
```php
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
```
allows(string $type): bool .[method]
------------------------------------
The `allows()` method checks type compatibility. For example, it can determine if a value of a certain type could be passed as a parameter to a function expecting this type.
```php
$type = Type::fromString('string|null');
$type->allows('string'); // true
$type->allows('null'); // true
$type->allows('Foo'); // false
$type = Type::fromString('mixed');
$type->allows('null'); // true
```
{{maintitle: PHP Type Analysis with Nette Utils: Compare and Inspect}}
{{description: The Type class represents a PHP data type from a string or reflection. Analyze union and intersection types and test their compatibility.}}
PHP Type
Nette\Utils\Type is a
class for working with PHP data types.
Installation:
composer require nette/utils
All examples assume the following class alias is defined:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
This static method creates a Type object based on reflection. The parameter can be a ReflectionMethod
or ReflectionFunction object (returns the return value type), or a ReflectionParameter or
ReflectionProperty object. It resolves self, static, and parent to the actual
class name. If the subject has no type, it returns null.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
This static method creates a Type object from its string representation.
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames(): (string|array)[]
Returns an array of strings representing the subtypes that make up a compound type. For intersection types, it returns an array
of arrays.
$type = Type::fromString('string|null'); // or '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
Returns an array of Type objects representing the subtypes that make up a compound type:
$type = Type::fromString('string|null'); // or '?string'
$type->getTypes(); // [Type::fromString('string'), Type::fromString('null')]
$type = Type::fromString('(Foo&Bar)|string');
$type->getTypes(); // [Type::fromString('Foo&Bar'), Type::fromString('string')]
$type = Type::fromString('Foo&Bar');
$type->getTypes(); // [Type::fromString('Foo'), Type::fromString('Bar')]
getSingleName(): ?string
For simple types (including simple nullable types like ?string), returns the type name. Otherwise,
returns null.
$type = Type::fromString('string|null');
echo $type; // '?string'
echo $type->getSingleName(); // 'string'
$type = Type::fromString('?Foo');
echo $type; // '?Foo'
echo $type->getSingleName(); // 'Foo'
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
echo $type->getSingleName(); // null (it's a union type)
isSimple(): bool
Returns whether it is a simple type. Simple types also include simple nullable types (e.g.,
?string, ?Foo):
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // or 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true (because it contains null)
isUnion(): bool
Returns whether it is a union type (contains |).
$type = Type::fromString('Foo&Bar');
$type->isUnion(); // true
isIntersection(): bool
Returns whether it is an intersection type (contains &).
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
Returns whether the type is simple and also a PHP built-in type (like string, int,
array, callable, etc.).
$type = Type::fromString('string');
$type->isBuiltin(); // true
$type = Type::fromString('string|int');
$type->isBuiltin(); // false
$type = Type::fromString('Foo');
$type->isBuiltin(); // false
isClass(): bool
Returns whether the type is simple and also a class name (not a built-in type like string
or int).
$type = Type::fromString('string');
$type->isClass(); // false
$type = Type::fromString('Foo|null');
$type->isClass(); // true
$type = Type::fromString('Foo|Bar');
$type->isClass(); // false
isClassKeyword():
bool
Returns whether the type is one of the internal keywords self, parent, or static.
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
The allows() method checks type compatibility. For example, it can determine if a value of a certain type could be
passed as a parameter to a function expecting this type.
$type = Type::fromString('string|null');
$type->allows('string'); // true
$type->allows('null'); // true
$type->allows('Foo'); // false
$type = Type::fromString('mixed');
$type->allows('null'); // true