PHP típus

Nette\Utils\Type egy PHP adattípus osztály.

Telepítés:

composer require nette/utils

Minden példa feltételezi, hogy a következő osztály alias van definiálva:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

A statikus metódus létrehoz egy Type objektumot a tükrözés alapján. A paraméter lehet egy ReflectionMethod vagy ReflectionFunction objektum (visszaadja a visszatérési érték típusát) vagy egy ReflectionParameter vagy ReflectionProperty objektum. Feloldja a self, static és parent a tényleges osztálynévre. Ha a tárgynak nincs típusa, akkor a null értéket adja vissza.

class DemoClass
{
	public self $foo;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'

fromString(string $type): Type

A statikus metódus létrehozza a Type objektumot a szöveges jelölésnek megfelelően.

$type = Type::fromString('Foo|Bar');
echo $type;      // 'Foo|Bar'

getNames(): (string|array)[]

Visszaadja az összetett típust alkotó altípusok tömbjét stringként.

$type = Type::fromString('string|null'); // nebo '?string'
$type->getNames();  // ['string', 'null']

$type = Type::fromString('(Foo&Bar)|string');
$type->getNames();  // [['Foo', 'Bar'], 'string']

getTypes(): Type[]

Visszaadja az összetett típust alkotó altípusok tömbjét Type objektumként:

$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

Egyszerű típusok esetén visszaadja a típus nevét, egyébként 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

isSimple(): bool

Visszaadja, hogy egyszerű típusról van-e szó. Az egyszerű nullázható típusok is egyszerű típusoknak tekintendők:

$type = Type::fromString('string');
$type->isSimple();       // true
$type->isUnion();        // false

$type = Type::fromString('?Foo'); // nebo 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Visszaadja, hogy union típusról van-e szó.

$type = Type::fromString('Foo&Bar');
$type->isUnion();        // true

isIntersection(): bool

Visszaadja, hogy metszet típus-e.

$type = Type::fromString('string&int');
$type->isIntersection(); // true

isBuiltin(): bool

Visszaadja, hogy a típus egyszerre egyszerű és PHP beépített típus-e.

$type = Type::fromString('string');
$type->isBuiltin(); // true

$type = Type::fromString('string|int');
$type->isBuiltin(); // false

$type = Type::fromString('Foo');
$type->isBuiltin(); // false

isClass(): bool

Visszaadja, hogy a típus egyszerre egyszerű és osztálynév.

$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

Meghatározza, hogy a típus a self, parent, static belső típusok egyike-e. .

$type = Type::fromString('self');
$type->isClassKeyword();   // true

$type = Type::fromString('Foo');
$type->isClassKeyword();   // false

allows(string $type): bool

A allows() módszer ellenőrzi a típus kompatibilitást. Például lehetővé teszi annak ellenőrzését, hogy egy bizonyos típusú érték átadható-e paraméterként.

$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
verzió: 4.0