Helper Functions

Nette\Utils\Helpers is a static class with useful functions not included in other utils classes available since nette/utils 3.1.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Helpers;

capture(callable $cb): string

Executes a callback and returns the captured output as a string.

$res = Helpers::capture(function () use ($template) {
	$template->render();
});

getLastError(): string

Returns the last PHP error or empty string.

echo Helpers::getLastError();

falseToNull(mixed $val): mixed

Converts false to` null`, does not change other values.

echo Helpers::falseToNull(false); // null
echo Helpers::falseToNull(123); // 123

getSuggestion(string[] $possibilities, string $value): ?string

It looks for the one of $possibilities that is closest to $value but not the same.

echo Helpers::getSuggestion(['foo', 'bar', 'baz'], 'fo'); // 'foo'