GetPrintableType::of()

Since version 1.1.0

Description

GetPrintableType::of() returns the PHP type of the data that you pass to it.

It is designed for use in error logging and exception messages. It is a replacement for PHP's built-in gettype() function.

use GanbaroDigital\MissingBits\TypeInspectors\GetPrintableType;
public static string GetPrintableType::of(mixed $item, int $flags = GetPrintableType::FLAGS_DEFAULT);

Parameters

GetPrintableType::of() takes the following parameters:

The following flags are supported:

Flag Behaviour
GetPrintableType::FLAG_NONE no extra information required (default behaviour)
GetPrintableType::FLAG_CLASSNAME add the class or interface name if $item is a PHP object
GetPrintableType::FLAG_CALLABLE_DETAILS add the class (if present) and function or method name of the callable
GetPrintableType::FLAG_SCALAR_VALUE adds the value of $item if $item is a boolean, double, integer or string

If $flags is not provided, it defaults to GetPrintableType::FLAG_DEFAULTS. This contains the following flag(s):

Return Value

GetPrintableType::of() returns a string, which contains the PHP type of $item:

No Flags If Flag Then
NULL n/a n/a
array n/a n/a
boolean GetPrintableType::FLAG_SCALAR_VALUE boolean<true> or boolean<false>
callable GetPrintableType::FLAG_CALLABLE_DETAILS callable<function> or callable<classname::method>
double GetPrintableType::FLAG_SCALAR_VALUE double<value>
integer GetPrintableType::FLAG_SCALAR_VALUE integer<value>
object GetPrintableType::FLAG_CLASSNAME object<classname>
resource n/a n/a
string GetPrintableType::FLAG_SCALAR_VALUE string<value>

Callables over Arrays and Strings

If you pass in an array that is also a valid callable, GetPrintableType::of() will return callable.

The same goes for strings. If you pass in a string that is also a valid callable, GetPrintableType::of() will return callable.

Callables over Closure

PHP turns anonymous functions into objects of type Closure. GetPrintableType::of() will return callable instead of object<closure>.

PHP 7.0, gettype() and Strict Types

GetPrintableType::of() is built on top of PHP's built-in gettype() function.

PHP 7.0 introduced strict type checking for functions. Unfortunately, gettype() in PHP 7.0 doesn't return these new strict types. (We assume this is to preserve backwards-compatibility).

At some point in the future, we'll introduce our own get_strict_type() function, and add a GetPrintableType::FLAG_STRICT_TYPES to make use of it.

Throws

GetPrintableType::of() does not throw any exceptions.

Notes

None at this time.

Changelog

v1.6.1