callables
Table of Contents
Functions
- chainCallables() : callable|null
- Chains multiple callables to be executed in sequence.
- isCallable() : bool
- Checks whether a string can be resolved into a valid callable.
- memoizeCallable() : callable
- Memoizes a callable's result (caches based on arguments).
- middlewareCallable() : callable
- Wrap a callable with before/after middleware.
- resolveCallable() : callable|null
- Resolves a string callable into an actual callable.
- wrapCallable() : callable
- Wraps a callable to apply middleware/decorators before/after execution.
Functions
chainCallables()
Chains multiple callables to be executed in sequence.
chainCallables([array<string|int, mixed> $callables = [] ]) : callable|null
Creates a pipeline where each callable's output becomes the next callable's input. The first callable receives the arguments passed to the chain; subsequent callables receive only the result from the previous callable.
Returns null if the array is empty or if any callable cannot be resolved. Execution stops at the first unresolvable callable in the chain.
Parameters
- $callables : array<string|int, mixed> = []
-
An array of callables to execute in sequence. Each can be a string, array, object, or Closure.
Tags
Return values
callable|null —A new callable that executes the chain, or null if invalid
isCallable()
Checks whether a string can be resolved into a valid callable.
isCallable(string $callable) : bool
This is a convenience wrapper around resolveCallable() that returns a boolean
instead of the callable itself. Useful for validation or conditional logic.
Supports the same callable formats as resolveCallable():
- Fully qualified function names:
'oihana\core\normalize' - Static method notation:
'MyClass::method'or'MyNamespace\MyClass::method'
Parameters
- $callable : string
-
The callable as a string to check
Tags
Return values
bool —True if the callable can be resolved, false otherwise
memoizeCallable()
Memoizes a callable's result (caches based on arguments).
memoizeCallable(callable $callable) : callable
Returns a new callable that caches results based on the arguments passed. Subsequent calls with the same arguments return the cached result instead of re-executing the original callable. This improves performance for expensive computations with repeated identical arguments.
Cache keys are generated by serializing arguments using serialize().
Works with scalar values, arrays, and objects that support serialization.
WARNING: The cache grows indefinitely. For long-running processes with many unique argument combinations, consider clearing the cache or using a size limit.
Parameters
- $callable : callable
-
The original callable to memoize
Tags
Return values
callable —A new callable with memoization enabled
middlewareCallable()
Wrap a callable with before/after middleware.
middlewareCallable(callable $callable[, callable|array<string|int, callable> $before = [] ][, callable|array<string|int, callable> $after = [] ]) : callable
This function allows decorating a callable with logic that runs
before and/or after the original callable. Supports a single callable
or an array of callables for both before and after.
beforecallables receive the original arguments.aftercallables receive the original arguments + the result. If an after callback returns a non-null value, it overrides the result.
Parameters
- $callable : callable
-
The main callable to wrap
- $before : callable|array<string|int, callable> = []
-
Callable or array of callables to run before
- $after : callable|array<string|int, callable> = []
-
Callable or array of callables to run after
Tags
Return values
callable —Wrapped callable with middleware applied
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
__invokemethod - 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
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.
wrapCallable()
Wraps a callable to apply middleware/decorators before/after execution.
wrapCallable(callable $callable, callable $wrapper) : callable
This function allows you to decorate a callable with custom logic that executes before and/or after the original callable is invoked. The wrapper receives the original callable and its arguments, giving complete control over execution flow.
The wrapper callable receives:
- First argument: The original callable to invoke
- Remaining arguments: The arguments passed to the wrapped callable
The wrapper is responsible for calling the original callable and returning its result. This enables use cases like logging, timing, error handling, caching, etc.
Parameters
- $callable : callable
-
The original callable to wrap
- $wrapper : callable
-
A callable that receives the original callable and args. Signature:
function($original, ...$args)
Tags
Return values
callable —A new wrapped callable that applies the wrapper logic