Skip to content

Core\Reflect\ClassInfo::hasProperty

$classInfo->hasProperty(string $name): bool$classInfo->hasProperty(
    string $name,
): bool

Checks whether a class has a property with a given name.

hasProperty($name) returns true when the described class has a property called $name, and false when it has none. Properties the class inherits from a parent class count too. The name must match exactly, so Owner does not find a property called owner.

hasProperty also returns true for a private property. That property still cannot be read from outside its class. get throws a RuntimeError for a private property and a LogicError for a name that does not exist, so hasProperty is how a program tells the two cases apart.

The examples below check a few names, tell a private property from a missing one, and check the property names a client asked for before the program reads them.

Parameters

  • $namestringThe property's name without the $, as the declaration writes it.

Return value

Returns bool. true for a private or protected property as well as a public one. So you can tell a property you may not read from a misspelled name. properties tells you whether this code may read it.

Examples

Check for a property by name

<?nvs
// Shows how to check whether a class has a property with a given name.
class Entry
{
public string $title = '';
}
class Post extends Entry
{
public string $body = '';
}
var $found = Core\Reflect::forClass('Post');
if ($found != null) {
Core\Reflect\ClassInfo $info = $found;
// `hasProperty` returns `true` or `false`. This prints `yes`, `yes` and `no`.
// `title` comes from the parent class `Entry`.
array<string> $names = ['body', 'title', 'author'];
foreach ($names as string $name) {
echo $name, ': ', $info->hasProperty($name) ? 'yes' : 'no', "\n";
}
// Property names are case-sensitive, so this prints `no`.
echo 'Title: ', $info->hasProperty('Title') ? 'yes' : 'no', "\n";
}
Output
body: yes
title: yes
author: no
Title: no

A private property exists but cannot be read

<?nvs
// Shows the difference between a private property and a property that does not exist.
class Account
{
public int $balance = 120;
private string $password = 'secret';
}
class Try
{
public static function read(string $name): tainted string
{
var $info = Core\Reflect::forClass('Account');
if ($info == null) {
return 'no class';
}
// `hasProperty` returns `true` for a private property too.
if (!$info->hasProperty($name)) {
return $name . ': there is no such property';
}
try {
return $name . ': ' . ($info->get(new Account(), $name) as string);
} catch (RuntimeError $error) {
// `get` throws a `RuntimeError` for a property this code cannot read.
return $name . ': the property exists, but it is private';
}
}
}
// This prints one line for each name.
echo Try::read('balance'), "\n";
echo Try::read('password'), "\n";
echo Try::read('pasword'), "\n";
Output
balance: 120
password: the property exists, but it is private
pasword: there is no such property

Check the properties a client asked for

<?nvs
// Shows how an API checks the property names a client asked for before it reads them.
class Product
{
public string $sku = 'A-100';
public string $name = 'Desk lamp';
public string $colour = 'black';
}
class Fields
{
public static function pick(Product $product, array<string> $asked): tainted string
{
var $info = Core\Reflect::forObject($product);
tainted string $out = '';
foreach ($asked as string $field) {
// A property the class does not have is reported to the client, and nothing is read.
if (!$info->hasProperty($field)) {
$out = $out . 'unknown property: ' . $field . "\n";
continue;
}
$out = $out . $field . '=' . ($info->get($product, $field) as string) . "\n";
}
return $out;
}
}
// The client asked for `?fields=name,price,colour`. This prints the name, an error for
// `price`, and the colour.
echo Fields::pick(new Product(), ['name', 'price', 'colour']);
Output
name=Desk lamp
unknown property: price
colour=black