Skip to content

Core\Reflect\PropertyInfo::type

$propertyInfo->type(): ?string

Returns the declared type of one property of a class, as text.

type() returns the type written the same way the class writes it: int, ?int for an int that may be null, array<string>, or the name of a class. A property declared in a source file always has a type, so the result is text. The properties of the built-in error classes, such as message on LogicError, have no written type, so for them the result is null.

The result is the text of the declaration. It is not a value you can check a value against. To check the type of a value, use Core\Reflect::typeOf.

The examples below print the type of each property, find the properties that may be null, and convert the text of a web form to the type each property needs.

Return value

Returns ?string. The type as a string, such as int, ?int, array<string> or Customer. The properties of a built-in error class, such as message on LogicError, have no written type, so the result is null. To find the type of a value, use Core\Reflect::typeOf.

Changelog

VersionChange
0.0.6The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request.

Examples

Print the type of each property

<?nvs
// Shows how to print the declared type of each property of a class.
class Order
{
public int $id = 1042;
public float $total = 59.9;
public array<string> $items = [];
public ?string $coupon = null;
}
// This prints `id: int`, `total: float`, `items: array<string>` and `coupon: ?string`.
foreach (Core\Reflect::forObject(new Order())->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), ': ', $property->type() ?? 'no type', "\n";
}
Output
id: int
total: float
items: array<string>
coupon: ?string

Find the properties that may be null

<?nvs
// Shows how to find the properties that may be `null`, and a property with no type.
class Customer
{
public string $name = 'Ada';
public ?string $phone = null;
public ?Customer $referredBy = null;
}
// A nullable type starts with `?`. This prints `phone` and `referredBy`.
foreach (Core\Reflect::forObject(new Customer())->properties() as Core\Reflect\PropertyInfo $property) {
string $type = $property->type() ?? '';
if (Core\Str::startsWith($type, '?')) {
echo $property->name(), "\n";
}
}
// `message` on `LogicError` has no written type, so the result is `null`. This prints `none`.
foreach (Core\Reflect::forObject(new LogicError('failed'))->properties() as Core\Reflect\PropertyInfo $field) {
if ($field->name() == 'message') {
echo $field->type() ?? 'none', "\n";
}
}
Output
phone
referredBy
none

Convert form input to each type

<?nvs
// Converts the text of a web form to the type that each property of a class needs.
class Signup
{
public string $name = '';
public int $age = 0;
public bool $newsletter = false;
}
// A form sends every value as text.
array<string> $form = ['name' => 'Ada', 'age' => '36', 'newsletter' => '1'];
var $signup = new Signup();
var $info = Core\Reflect::forObject($signup);
// The loop reads the type of each property and converts the text to that type.
foreach ($info->properties() as Core\Reflect\PropertyInfo $property) {
string $text = $form[$property->name()];
string $type = $property->type() ?? 'string';
if ($type == 'int') {
$info->set($signup, $property->name(), $text as int);
} elseif ($type == 'bool') {
$info->set($signup, $property->name(), $text == '1');
} else {
$info->set($signup, $property->name(), $text);
}
}
// This prints `Ada`, `37` and `yes`.
echo $signup->name, "\n";
echo $signup->age + 1, "\n";
echo $signup->newsletter ? 'yes' : 'no', "\n";
Output
Ada
37
yes