getCallableType.php
Table of Contents
Functions
- getCallableType() : string|false
- Determines the type of a callable reference and optionally normalizes it.
Functions
getCallableType()
Determines the type of a callable reference and optionally normalizes it.
getCallableType(mixed $callable[, bool $strict = true ][, callable|null &$norm = null ]) : string|false
This function analyzes a PHP callable and returns its standardized type according to the constants defined in CallableType. It can also normalize the callable form for consistent usage throughout your application.
Callable Types and Normalization
| Callable | Normalization | Returned Type |
|---|---|---|
function (...) {...} |
function (...) {...} |
'closure' |
$object (with __invoke) |
$object |
'invocable' |
"function" |
"function" |
'function' |
"Class::method" |
["Class", "method"] |
'static' |
["Class", "parent::method"] |
["ParentClass", "method"] |
'static' |
["Class", "self::method"] |
["Class", "method"] |
'static' |
["Class", "method"] |
["Class", "method"] |
'static' |
[$object, "parent::method"] |
[$object, "parent::method"] |
'object' |
[$object, "self::method"] |
[$object, "method"] |
'object' |
[$object, "method"] |
[$object, "method"] |
'object' |
| Other recognized callable | Same as input | 'unknown' |
| Non-callable | Same as input | false |
Strict Mode
When the $strict parameter is set to true, additional checks are performed
using the Reflection API:
- For callable strings like
"Class::method"or arrays like["Class", "method"], the method must be declared as static. - For callable arrays like
[$object, "method"], the method must be an instance method (non-static).
In non-strict mode ($strict = false), these checks are skipped and the type
is determined solely by the callable's structure.
Usage Examples
// Closure
$type = getCallableType(fn() => 42, false, $norm);
// Returns: 'closure', $norm = the closure itself
// Named function
$type = getCallableType('strlen', false, $norm);
// Returns: 'function', $norm = 'strlen'
// Static method (string form)
$type = getCallableType('MyClass::myMethod', false, $norm);
// Returns: 'static', $norm = ['MyClass', 'myMethod']
// Static method (array form)
$type = getCallableType(['MyClass', 'myMethod'], false, $norm);
// Returns: 'static', $norm = ['MyClass', 'myMethod']
// Instance method
$obj = new MyClass();
$type = getCallableType([$obj, 'myMethod'], false, $norm);
// Returns: 'object', $norm = [$obj, 'myMethod']
// Invocable object
$invocable = new class {
public function __invoke() { return 'hello'; }
};
$type = getCallableType($invocable, false, $norm);
// Returns: 'invocable', $norm = $invocable
// Non-callable
$type = getCallableType(123, false, $norm);
// Returns: false, $norm = 123
Parameters
- $callable : mixed
-
The callable reference to analyze.
- $strict : bool = true
-
When
true, uses Reflection to verify that static/instance methods are used correctly. Defaults totrue. - $norm : callable|null = null
-
Receives the normalized form of the callable (passed by reference). If the callable is invalid, contains the original value.
Tags
Return values
string|false —One of CallableType::CLOSURE, CallableType::INVOCABLE,
CallableType::FUNCTION, CallableType::STATIC,
CallableType::OBJECT, CallableType::UNKNOWN,
or false if not a callable.