Core\Reflect\ConstantInfo::isPublic
$constantInfo->isPublic(): boolTells you whether code outside the class may read a class constant.
isPublic() returns true for a public constant. It returns false for a protected or private constant. Those constants are still in the list that Core\Reflect\ClassInfo::constants returns, so you can see that they exist.
When isPublic() returns false and code outside the class passes that name to Core\Reflect\ClassInfo::constant, constant throws a RuntimeError. Code inside the class can still read the constant.
The examples below show which constants of a class are public, what happens when you read one that is not, and how to print only the public settings of a class.
Return value
Returns bool. false for a private or protected constant. That constant is still in the list, as with Core\Reflect\PropertyInfo::isPublic. So you can tell a constant you may not read from a misspelled name.
Examples
Show which constants are public
<?nvs// Shows how to check which constants of a class are public.
class Mailer{ public const string FROM = 'shop@example.com'; protected const int RETRIES = 3; private const string PASSWORD = 'letmein';}
var $info = Core\Reflect::forObject(new Mailer());
// `isPublic` is `true` only for `FROM`.// The protected and the private constant are in the list, but `isPublic` is `false` for them.foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { echo $constant->name(), ': ', $constant->isPublic() ? 'public' : 'not public', "\n";}FROM: publicRETRIES: not publicPASSWORD: not publicReading a constant that is not public
<?nvs// Shows what happens when you read a constant that is not public, and how `isPublic` avoids it.
class Mailer{ public const int PORT = 587; private const string PASSWORD = 'letmein';}
var $info = Core\Reflect::forObject(new Mailer());
// `PASSWORD` is private, so `constant` throws a `RuntimeError` here.try { $info->constant('PASSWORD');} catch (RuntimeError $closed) { echo "PASSWORD cannot be read here\n";}
// Check `isPublic` first, and the loop reads only the constants it may read.// This prints "PORT = 587".foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { if ($constant->isPublic()) { echo $constant->name(), ' = ', $info->constant($constant->name()) as int, "\n"; }}PASSWORD cannot be read herePORT = 587Print the public settings
<?nvs// Shows how to print the public limits of a class on a status page, and hide the private ones.
class Limits{ public const int MAX_UPLOAD_MB = 20; public const int MAX_USERS = 500; private const int INTERNAL_QUOTA = 90000;}
// This method prints the limits of any class it is given.class StatusPage{ public static function printLimits(Core\Reflect\ClassInfo $info): void { echo 'Limits of ', $info->name(), ":\n"; foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { // A private limit is skipped. It does not appear on the page. if (!$constant->isPublic()) { continue; } echo ' ', $constant->name(), ': ', $info->constant($constant->name()) as int, "\n"; } }}
StatusPage::printLimits(Core\Reflect::forObject(new Limits()));Limits of Limits: MAX_UPLOAD_MB: 20 MAX_USERS: 500