Nette Documentation Preview

syntax
Arbeiten mit Callbacks
**********************

.[perex]
[api:Nette\Utils\Callback] ist eine statische Klasse mit Funktionen für die Arbeit mit [PHP Callbacks |https://www.php.net/manual/en/language.types.callable.php].


Installation:

```shell
composer require nette/utils
```

Alle Beispiele setzen voraus, dass ein Alias erstellt wurde:

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


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

Überprüft, ob die Variable `$callable` ein gültiger Callback ist. Andernfalls wird `Nette\InvalidArgumentException` geworfen. Wenn `$syntax` true ist, überprüft die Funktion nur, ob `$callable` die Struktur eines Callbacks hat, überprüft aber nicht, ob die angegebene Klasse oder Methode tatsächlich existiert. Gibt `$callable` zurück.

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


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

Konvertiert einen PHP-Callback in eine Textform. Die Klasse oder Methode muss nicht existieren.

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


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

Gibt die Reflexion für eine Methode oder Funktion in einem PHP-Callback zurück.

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

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


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

Stellt fest, ob ein PHP-Callback eine Funktion oder eine statische Methode ist.

```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]
--------------------------------------------------

Packt eine Closure, die mit `Closure::fromCallable`:https://www.php.net/manual/en/closure.fromcallable.php erstellt wurde, wieder aus.

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

Arbeiten mit Callbacks

Nette\Utils\Callback ist eine statische Klasse mit Funktionen für die Arbeit mit PHP Callbacks.

Installation:

composer require nette/utils

Alle Beispiele setzen voraus, dass ein Alias erstellt wurde:

use Nette\Utils\Callback;

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

Überprüft, ob die Variable $callable ein gültiger Callback ist. Andernfalls wird Nette\InvalidArgumentException geworfen. Wenn $syntax true ist, überprüft die Funktion nur, ob $callable die Struktur eines Callbacks hat, überprüft aber nicht, ob die angegebene Klasse oder Methode tatsächlich existiert. Gibt $callable zurück.

Callback::check('trim'); // wirft keine Ausnahme
Callback::check(['NonExistentClass', 'method']); // wirft Nette\InvalidArgumentException
Callback::check(['NonExistentClass', 'method'], true); // wirft keine Ausnahme
Callback::check(function () {}); // wirft keine Ausnahme
Callback::check(null); // wirft Nette\InvalidArgumentException

toString($callable): string

Konvertiert einen PHP-Callback in eine Textform. Die Klasse oder Methode muss nicht existieren.

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

toReflection($callable): ReflectionMethod|ReflectionFunction

Gibt die Reflexion für eine Methode oder Funktion in einem PHP-Callback zurück.

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

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

isStatic($callable): bool

Stellt fest, ob ein PHP-Callback eine Funktion oder eine statische Methode ist.

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

unwrap(Closure $closure): callable|array

Packt eine Closure, die mit Closure::fromCallable erstellt wurde, wieder aus.

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