Core\Reflect\ClassInfo::constant
Reads the value of a class constant when the constant's name is in a string.
constant($name) returns the value of the constant $name, including one the class inherits. The result has the type mixed, so convert it with as. A constant can be read only where a normal read of it would be allowed: a private constant cannot be read from outside its class.
Two kinds of error are thrown. A name the class has no constant for throws a LogicError. A constant you may not read throws a RuntimeError. A constant declared secret is never returned, not even inside its own class, because a mixed value cannot stay secret.
The examples below show two constants read by name, the two errors, and a plan's limit read from the plan name stored with a customer.
Parameters
$namestringThe constant's name, as the declaration writes it, with no class name and no::. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns mixed. The value, computed at compile time. It is a string, int, bool or float, the same type as the value in the declaration.
Errors
RuntimeErrorCode at this call site may not read the constant$name. Reflection has the same visibility rules as ordinary code. This error is also thrown everywhere for a constant whose declared type issecret. Themixedresult cannot besecret, so the value would lose that protection.LogicErrorThe class declares and inherits no constant of that name. Or the constant's value is not astring,int,boolorfloatwritten directly in the code.hasValuechecks this before the call. Both are mistakes in the program, not permission problems. That is how they differ from theRuntimeErrorabove.
Changelog
| Version | Change |
|---|---|
0.0.6 | The 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
Read a constant by name
<?nvs// Shows how to read a class constant when its name is in a string.
class Limits{ public const int MAX_UPLOAD_MB = 20; public const string UNIT = 'MB';}
var $info = Core\Reflect::forObject(new Limits());
// `constant` returns the value of the constant with that name.// The result has the type `mixed`, so `as` converts it. This prints "20 MB".int $size = $info->constant('MAX_UPLOAD_MB') as int;tainted string $unit = $info->constant('UNIT') as string;echo $size, ' ', $unit, "\n";20 MBPrivate and unknown constants
<?nvs// Shows the two errors `constant` can throw.
class Service{ public const string NAME = 'billing'; private const string API_KEY = 'k-123';}
var $info = Core\Reflect::forObject(new Service());
// A public constant works. This prints "billing".echo $info->constant('NAME') as string, "\n";
// `API_KEY` is private, so code outside the class cannot read it.// `Service::API_KEY` does not compile here, and `constant` throws a `RuntimeError`.try { $info->constant('API_KEY');} catch (RuntimeError $closed) { echo "API_KEY is private\n";}
// The class has no constant named `VERSION`, so `constant` throws a `LogicError`.try { $info->constant('VERSION');} catch (LogicError $missing) { echo "no constant VERSION\n";}billingAPI_KEY is privateno constant VERSIONLimits for each plan
<?nvs// Shows how a web shop reads the limit of a customer's plan from a class// constant, when the plan name is stored with the customer.
class PlanLimits{ public const int FREE = 3; public const int TEAM = 25; public const int BUSINESS = 200;}
class Plans{ // Returns the number of projects a plan allows, or 0 for an unknown plan. public static function projects(string $plan): int { var $info = Core\Reflect::forObject(new PlanLimits()); try { return $info->constant($plan) as int; } catch (LogicError $unknown) { return 0; } }}
// These plan names come from the customer records.// This prints "FREE: 3", "TEAM: 25" and "GOLD: 0".array<string> $plans = ['FREE', 'TEAM', 'GOLD'];foreach ($plans as string $plan) { echo $plan, ': ', Plans::projects($plan), "\n";}FREE: 3TEAM: 25GOLD: 0