Nette Documentation Preview

syntax
Callback Functions
******************

.[perex]
[api:Nette\Utils\Callback] is a static class, which contains functions for working with [PHP callbacks |https://www.php.net/manual/en/language.types.callable.php].

→ [Installation and requirements |@home#Installation]

All examples assume the following class alias is defined:

```php
use Nette\Utils\Callback;
```


check($callable, bool $syntax = false): callable .[method]
----------------------------------------------------------

Checks that `$callable` is valid PHP callback. Otherwise throws `Nette\InvalidArgumentException`. If the `$syntax` is set to true, the function only verifies that `$callable` has a valid structure to be used as a callback, but does not verify if the class or method actually exists. Returns `$callable`.

```php
Callback::check('trim'); // no exception
Callback::check(['NonExistentClass', 'method']); // throws Nette\InvalidArgumentException
Callback::check(['NonExistentClass', 'method'], true); // no exception
Callback::check(function () {}); // no exception
Callback::check(null); // throws Nette\InvalidArgumentException
```


toString($callable): string .[method]
-------------------------------------

Converts PHP callback to textual form. Class or method may not exists.

```php
Callback::toString('trim');                // 'trim'
Callback::toString(['MyClass', 'method']); // 'MyClass::method'
```


toReflection($callable): ReflectionMethod|ReflectionFunction .[method]
----------------------------------------------------------------------

Returns reflection for method or function used in PHP callback.

```php
$ref = Callback::toReflection('trim');
// $ref is ReflectionFunction('trim')

$ref = Callback::toReflection(['MyClass', 'method']);
// $ref is ReflectionMethod('MyClass', 'method')
```


isStatic($callable): bool .[method]
-----------------------------------

Checks whether PHP callback is function or static method.

```php
Callback::isStatic('trim');                // true
Callback::isStatic(['MyClass', 'method']); // true
Callback::isStatic([$obj, 'method']);      // false
Callback::isStatic(function () {});        // false
```


unwrap(Closure $closure): callable|array .[method]
--------------------------------------------------

Unwraps closure created by `Closure::fromCallable`:https://www.php.net/manual/en/closure.fromcallable.php.

```php
$closure = Closure::fromCallable(['MyClass', 'method']);
Callback::unwrap($closure);     // ['MyClass', 'method']
```


closure($callable): Closure .[method deprecated]
------------------------------------------------

(or `closure($classOrObject, $method): Closure`)

Converts PHP callback to object "Closure":https://www.php.net/manual/en/class.closure.php.

```php
$trimmer = Callback::closure('trim');
$trimmer(' hello '); // 'hello'
```

.[note]
Since PHP 7.1 you should use native `Closure::fromCallable`:https://www.php.net/manual/en/closure.fromcallable.php.


invoke($callable, ...$args): mixed .[method deprecated]
-------------------------------------------------------

Calls the `$callable`, passes the remaining parameters as arguments and returns the returned value.

When is not callable, it throws `Nette\InvalidArgumentException` unlike `call_user_func` which only triggers a warning.

```php
$callback = 'trim';
Callback::invoke($callback, ' hello '); // 'hello'
```

.[note]
Since PHP 7.0 you should use native `$callback($arg, ...)`.


invokeArgs($callable, array $args = []): mixed .[method deprecated]
-------------------------------------------------------------------

Calls the `$callable` with `$args` arguments and return its value.

When is not callable, it throws `Nette\InvalidArgumentException` unlike `call_user_func` which only triggers a warning.

```php
$callback = 'trim';
Callback::invoke($callback, [' hello ']); // 'hello'
```

.[note]
Since PHP 7.0 you should use native `$callback(...$args)`.


{{composer: nette/utils}}

Callback Functions

Nette\Utils\Callback is a static class, which contains functions for working with PHP callbacks.

Installation and requirements

All examples assume the following class alias is defined:

use Nette\Utils\Callback;

check($callable, bool $syntax = false): callable

Checks that $callable is valid PHP callback. Otherwise throws Nette\InvalidArgumentException. If the $syntax is set to true, the function only verifies that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists. Returns $callable.

Callback::check('trim'); // no exception
Callback::check(['NonExistentClass', 'method']); // throws Nette\InvalidArgumentException
Callback::check(['NonExistentClass', 'method'], true); // no exception
Callback::check(function () {}); // no exception
Callback::check(null); // throws Nette\InvalidArgumentException

toString($callable): string

Converts PHP callback to textual form. Class or method may not exists.

Callback::toString('trim');                // 'trim'
Callback::toString(['MyClass', 'method']); // 'MyClass::method'

toReflection($callable): ReflectionMethod|ReflectionFunction

Returns reflection for method or function used in PHP callback.

$ref = Callback::toReflection('trim');
// $ref is ReflectionFunction('trim')

$ref = Callback::toReflection(['MyClass', 'method']);
// $ref is ReflectionMethod('MyClass', 'method')

isStatic($callable): bool

Checks whether PHP callback is function or static method.

Callback::isStatic('trim');                // true
Callback::isStatic(['MyClass', 'method']); // true
Callback::isStatic([$obj, 'method']);      // false
Callback::isStatic(function () {});        // false

unwrap(Closure $closure): callable|array

Unwraps closure created by Closure::fromCallable.

$closure = Closure::fromCallable(['MyClass', 'method']);
Callback::unwrap($closure);     // ['MyClass', 'method']

closure($callable): Closure

(or closure($classOrObject, $method): Closure)

Converts PHP callback to object Closure.

$trimmer = Callback::closure('trim');
$trimmer(' hello '); // 'hello'

Since PHP 7.1 you should use native Closure::fromCallable.

invoke($callable, …$args): mixed

Calls the $callable, passes the remaining parameters as arguments and returns the returned value.

When is not callable, it throws Nette\InvalidArgumentException unlike call_user_func which only triggers a warning.

$callback = 'trim';
Callback::invoke($callback, ' hello '); // 'hello'

Since PHP 7.0 you should use native $callback($arg, ...).

invokeArgs($callable, array $args = [])mixed

Calls the $callable with $args arguments and return its value.

When is not callable, it throws Nette\InvalidArgumentException unlike call_user_func which only triggers a warning.

$callback = 'trim';
Callback::invoke($callback, [' hello ']); // 'hello'

Since PHP 7.0 you should use native $callback(...$args).