PHP Tipi

Nette\Utils\Type, PHP veri tipleriyle çalışmak için bir sınıftır.

Kurulum:

composer require nette/utils

Tüm örnekler, bir takma ad oluşturulduğunu varsayar:

use Nette\Utils\Type;

fromReflection ($reflection): ?Type

Statik metot, yansımaya dayalı olarak bir Type nesnesi oluşturur. Parametre ReflectionMethod veya ReflectionFunction nesnesi (dönüş değerinin türünü döndürür) veya ReflectionParameter ya da ReflectionProperty olabilir. self, static ve parent'ı gerçek sınıf adına çevirir. Konunun bir türü yoksa null döndürür.

class DemoClass
{
	public self $foo;
}

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

fromString (string $type): Type

Statik metot, metin gösterimine göre bir Type nesnesi oluşturur.

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

getNames (): (string|array)[]

Bileşik türü oluşturan alt türlerin dizisini karakter dizileri olarak döndürür.

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

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

getTypes(): Type[]

Bileşik türü oluşturan alt türlerin dizisini ReflectionType nesneleri olarak döndürür:

$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

Basit türler için tür adını döndürür, aksi takdirde 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

Basit bir tür olup olmadığını döndürür. Basit null atanabilir türler de basit türler olarak kabul edilir:

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

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

isUnion(): bool

Bir birleşim (union) türü olup olmadığını döndürür.

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

isIntersection(): bool

Bir kesişim (intersection) türü olup olmadığını döndürür.

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

isBuiltin(): bool

Türün basit ve aynı zamanda yerleşik bir PHP türü olup olmadığını döndürür.

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

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

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

isClass(): bool

Türün basit ve aynı zamanda bir sınıf adı olup olmadığını döndürür.

$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

Türün self, parent, static dahili türlerinden biri olup olmadığını döndürür.

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

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

allows (string $type): bool

allows() metodu tür uyumluluğunu doğrular. Örneğin, belirli bir türdeki bir değerin parametre olarak iletilip iletilemeyeceğini belirlemeye olanak tanır.

$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
versiyon: 4.0