2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\FunchandException;
|
|
|
|
|
|
|
|
/**
|
2022-07-12 19:26:21 +00:00
|
|
|
* Creates a function dynamically from the parameters passed, and returns a unique name for it.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $args The function arguments, as a single comma-separated string.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @param string $code The function code.
|
|
|
|
* @return string Returns a unique function name as a string.
|
2022-07-12 19:26:21 +00:00
|
|
|
* Note that the name contains a non-printable character ("\0"),
|
|
|
|
* so care should be taken when printing the name or incorporating it in any other
|
|
|
|
* string.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws FunchandException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function create_function(string $args, string $code): string
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \create_function($args, $code);
|
|
|
|
if ($result === false) {
|
|
|
|
throw FunchandException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param callable(): void $callback The function to register.
|
|
|
|
* @param mixed $args
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws FunchandException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function register_tick_function(callable $callback, ...$args): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
if ($args !== []) {
|
|
|
|
$result = \register_tick_function($callback, ...$args);
|
2021-02-26 16:16:17 +00:00
|
|
|
} else {
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \register_tick_function($callback);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
if ($result === false) {
|
|
|
|
throw FunchandException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|