Core\Reflect::typeOf
Core\Reflect::typeOf(mixed $value): Core\Reflect::TypeKindCore\Reflect::typeOf( mixed $value, ): Core\Reflect::TypeKind
Tells you what kind of value a variable contains while the program runs.
A variable of type mixed can contain any value: a number, a string, an array, an object or null. Core\Reflect::typeOf returns one case of the enum Core\Reflect::TypeKind, such as Int, Text or Array. Every value matches exactly one case. The answer is about the value in the variable now, so the same variable can give a different case after you assign something new to it.
On a variable with a narrower type, such as int, the compiler already knows the answer. typeOf is useful where the type is mixed.
The examples below show a label for each kind of value, one variable that changes kind, and a check of configuration values before a program uses them.
Parameters
$valuemixedThe value to check. For any type butmixed, the compiler already knows the result, so this is useful for a value whose type ismixed.
Return value
Returns Core\Reflect::TypeKind. One Core\Reflect::TypeKind case. Every value matches exactly one case, because the cases do not overlap.
Examples
A label for each kind
<?nvs// Shows how to find out what kind of value a `mixed` variable contains.
class Kind{ public static function label(mixed $value): string { var $kind = Core\Reflect::typeOf($value); if ($kind == Core\Reflect::TypeKind::Int) { return 'a whole number'; } if ($kind == Core\Reflect::TypeKind::Float) { return 'a float'; } if ($kind == Core\Reflect::TypeKind::Text) { return 'a string'; } if ($kind == Core\Reflect::TypeKind::Bool) { return 'true or false'; } if ($kind == Core\Reflect::TypeKind::Null) { return 'null'; } return 'something else'; }}
// This prints one line for each of the five values.array<mixed> $values = [42, 1.5, 'hello', true, null];foreach ($values as mixed $value) { echo Kind::label($value), "\n";}a whole numbera floata stringtrue or falsenullOne variable changes kind
<?nvs// Shows that `typeOf` checks the value a variable contains now, not its declared type.
class Show{ public static function isText(mixed $value): string { return Core\Reflect::typeOf($value) == Core\Reflect::TypeKind::Text ? 'string' : 'not a string'; }}
// The variable first contains a number, then a string, then an array.// This prints `not a string`, then `string`, then `not a string`.mixed $slot = 7;echo Show::isText($slot), "\n";$slot = 'seven';echo Show::isText($slot), "\n";$slot = [7, 7];echo Show::isText($slot), "\n";
// An array is one value, so `typeOf` returns `Array` for it.// It does not look at the values inside. This prints `array`.echo Core\Reflect::typeOf($slot) == Core\Reflect::TypeKind::Array ? 'array' : 'other', "\n";not a stringstringnot a stringarrayCheck settings before use
<?nvs// Shows how to check settings of unknown type before a program uses them.
class Settings{ public static function check(string $key, mixed $value, Core\Reflect::TypeKind $wanted): bool { if (Core\Reflect::typeOf($value) == $wanted) { echo $key, ": ok\n"; return true; } echo $key, ": wrong kind of value\n"; return false; }}
// The port must be a whole number, the host a string and `debug` true or false.// Here the port was written as a string, so the check for `port` fails.mixed $port = '8080';mixed $host = 'example.com';mixed $debug = false;
int $failed = 0;if (!Settings::check('port', $port, Core\Reflect::TypeKind::Int)) { $failed = $failed + 1;}if (!Settings::check('host', $host, Core\Reflect::TypeKind::Text)) { $failed = $failed + 1;}if (!Settings::check('debug', $debug, Core\Reflect::TypeKind::Bool)) { $failed = $failed + 1;}
// This prints `1 setting(s) to fix`.echo $failed, " setting(s) to fix\n";port: wrong kind of valuehost: okdebug: ok1 setting(s) to fix