Core\Reflect\PropertyInfo::isPublic
$propertyInfo->isPublic(): boolTells you whether code outside a class may read and write one of its properties.
isPublic() returns true for a public property. It returns false for a protected or private property. Those properties are still in the list that Core\Reflect\ClassInfo::properties returns, so you can see that they exist.
If isPublic() returns false, Core\Reflect\ClassInfo::get and Core\Reflect\ClassInfo::set throw a RuntimeError when you call them from outside the class. Checking isPublic() first lets your program skip those properties.
The examples below print which properties of a class are public, read only the public properties of an object, and list the fields that a web API may send back to a client.
Return value
Returns bool. true for a public property, and false for a protected or private property. Those properties are still in the list that Core\Reflect\ClassInfo::properties returns. If isPublic returns false, Core\Reflect\ClassInfo::get from outside the class throws a RuntimeError.
Examples
Show which properties are public
<?nvs// Shows which properties of a class are public.
class Account{ public string $email = 'ada@example.com'; protected int $loginCount = 4; private string $passwordHash = 'x9f2';}
// This prints `email: yes`, `loginCount: no` and `passwordHash: no`.foreach (Core\Reflect::forObject(new Account())->properties() as Core\Reflect\PropertyInfo $property) { echo $property->name(), ': ', $property->isPublic() ? 'yes' : 'no', "\n";}email: yesloginCount: nopasswordHash: noRead only the public properties
<?nvs// Shows how to read the public properties of an object and skip the others.
class Session{ public string $user = 'ada'; public int $expires = 3600; private string $token = 'a81c-44f0';}
var $session = new Session();var $info = Core\Reflect::forObject($session);
// `get` throws a `RuntimeError` for `token`, so the loop checks `isPublic` first.// This prints `user = ada` and `expires = 3600`.foreach ($info->properties() as Core\Reflect\PropertyInfo $property) { if ($property->isPublic()) { echo $property->name(), ' = ', $info->get($session, $property->name()) as string, "\n"; }}
// Reading `token` from outside the class throws an error. This prints `RuntimeError`.try { echo $info->get($session, 'token') as string, "\n";} catch (RuntimeError $error) { echo "RuntimeError\n";}user = adaexpires = 3600RuntimeErrorList the fields an api returns
<?nvs// Lists the fields that a web API sends back for a user. Only public properties are sent.
class User{ public int $id = 7; public string $name = 'Ada'; public string $email = 'ada@example.com'; private string $passwordHash = 'x9f2'; protected bool $isAdmin = false;}
// The loop keeps the name of each public property.array<string> $fields = [];foreach (Core\Reflect::forObject(new User())->properties() as Core\Reflect\PropertyInfo $property) { if ($property->isPublic()) { $fields[] = $property->name(); }}
// This prints `id`, `name` and `email`. The hash and the admin flag are not sent.foreach ($fields as string $field) { echo $field, "\n";}idnameemail