Core\Reflect\ConstantInfo::hasValue
$constantInfo->hasValue(): boolTells you whether Core\Reflect\ClassInfo::constant can return the value of a class constant.
hasValue() returns true when the constant's value is a string, int, bool or float written in the declaration. It returns false for an array constant. For such a constant, constant throws a LogicError, because it has no value to return.
hasValue() does not check who may read the constant. It returns true for a private constant with an int value, and constant can still throw a RuntimeError outside the class. isPublic is the check for that.
The examples below show which kinds of constant have a value, what happens when you read one that has none, and how to print the public settings of a class when some of them are lists.
Return value
Returns bool. true for a string, int, bool or float written directly in the code. false for an array or object constant, and for an integer too large for int. Novis reads a constant only when its value is written directly in the code, and it does not evaluate other expressions. So this is false for exactly the constants that are also not allowed in type position.
Examples
Which constants have a value
<?nvs// Shows which kinds of class constant have a value that `constant` can return.
class Settings{ public const string NAME = 'shop'; public const int PAGE_SIZE = 25; public const bool DEBUG = false; public const float TAX_RATE = 0.19; public const array<string> LANGUAGES = ['en', 'de'];}
var $info = Core\Reflect::forObject(new Settings());
// A `string`, `int`, `bool` or `float` constant has a value.// `hasValue` is `false` only for the array constant `LANGUAGES`.foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { echo $constant->name(), ': ', $constant->hasValue() ? 'has a value' : 'no value', "\n";}NAME: has a valuePAGE_SIZE: has a valueDEBUG: has a valueTAX_RATE: has a valueLANGUAGES: no valueAn array constant has no value
<?nvs// Shows what happens when you read a constant that has no value, and how `hasValue` avoids it.
class Upload{ public const int MAX_MB = 20; public const array<string> TYPES = ['png', 'jpg'];}
var $info = Core\Reflect::forObject(new Upload());
// `TYPES` is an array constant, so `constant` throws a `LogicError`.try { $info->constant('TYPES');} catch (LogicError $none) { echo "TYPES has no value to read\n";}
// Check `hasValue` first, and the loop reads only the constants that have a value.// This prints "MAX_MB = 20".foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { if ($constant->hasValue()) { echo $constant->name(), ' = ', $info->constant($constant->name()) as int, "\n"; }}TYPES has no value to readMAX_MB = 20Print every setting
<?nvs// Shows how to print every public setting of a class, when some settings are lists.
class Queue{ public const int WORKERS = 4; public const int TIMEOUT_SECONDS = 60; public const array<string> PRIORITIES = ['high', 'normal', 'low'];}
var $info = Core\Reflect::forObject(new Queue());
// A setting with a value prints it. A list prints "(a list)", because `constant` cannot return it.foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) { if (!$constant->isPublic()) { continue; } if ($constant->hasValue()) { echo $constant->name(), ': ', $info->constant($constant->name()) as int, "\n"; } else { echo $constant->name(), ": (a list)\n"; }}WORKERS: 4TIMEOUT_SECONDS: 60PRIORITIES: (a list)