Oihana PHP

resolveCallable.php

Table of Contents

Functions

resolveCallable()  : callable|null
Resolves a string callable into an actual callable.

Functions

resolveCallable()

Resolves a string callable into an actual callable.

resolveCallable(string|array<string|int, mixed>|object|null $callable) : callable|null

This function attempts to convert various callable representations into actual PHP callables that can be invoked directly. It validates the existence of functions and methods before returning them, ensuring the returned callable is executable.

Supports multiple callable formats:

  • Fully qualified function names: 'oihana\core\normalize'
  • Static method notation: 'MyClass::method' or 'MyNamespace\MyClass::method'
  • Array callables: [$object, 'method'] or ['ClassName', 'method']
  • Invokable objects: Objects implementing __invoke method
  • Closure instances and callable objects

Returns null if the callable cannot be resolved (function/method does not exist or format is invalid).

Parameters
$callable : string|array<string|int, mixed>|object|null

The callable candidate to resolve. Can be:

  • A string containing a function or method name
  • An array with object/class and method name
  • An object instance with __invoke method
  • A Closure instance
  • null
Tags
example
use function oihana\core\callables\resolveCallable;

// Function by fully qualified name
$fn = resolveCallable('oihana\core\normalize');
if ($fn !== null)
{
    $result = $fn($value);
}

// Static method
$fn = resolveCallable('MyNamespace\MyClass::transform');
if ($fn !== null)
{
    $result = $fn($data);
}

// Array callable with object instance
$handler = new MyHandler();
$fn = resolveCallable([$handler, 'handle']);

// Invokable object
$fn = resolveCallable(new MyCallable());

// Non-existent function returns null
$fn = resolveCallable('nonexistent\function'); // null
$fn = resolveCallable('NonExistentClass::method'); // null
see
is_callable()

For direct callable validation without resolution

author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
callable|null

The resolved callable that can be executed, or null if the callable cannot be resolved or does not exist in the current runtime.


        
On this page

Search results