note 126821 added to language.types.callable
| From: | chechomancr at hotmail dot com | Date: | Thu, 03 Feb 2022 10:09:12 +0000 |
| Subject: | note 126821 added to language.types.callable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217570@lists.php.net to get a copy of this message | ||
In PHP >= 8.0 callIt function with optional parameters.
<?php
declare(strict_types = 1);
class Example {
public static function someFunction(string $value, string $a_parameter, int $another_parameter):
string
{
//TODO: Apply the required filter
return "$value $a_parameter $another_parameter";
}
public static function callIt(string $callback, ...$parameters): callable
{
return static fn(mixed $value = null) => self::$callback($value, ...$parameters);
}
}
?>
How to use:
<?php
declare(strict_types = 1);
$args = [
'information' => ['filter' => FILTER_CALLBACK, 'options'
=> Example::callIt('someFunction', 'world', 2022)],
];
$unfiltered_data = ['information' => 'Hello'];
$filtered_data = filter_var_array($unfiltered_data, $args);
if(false === $filtered_data) {
echo "Error...", PHP_EOL;
}
echo var_export($filtered_data, true);
?>
Result: array ( 'information' => 'Hello world 2022' )
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 186.32.217.53)
----
Manual Page -- https://php.net/manual/en/language.types.callable.php
Edit -- https://main.php.net/note/edit/126821
Del: integrated -- https://main.php.net/note/delete/126821/integrated
Del: useless -- https://main.php.net/note/delete/126821/useless
Del: bad code -- https://main.php.net/note/delete/126821/bad+code
Del: spam -- https://main.php.net/note/delete/126821/spam
Del: non-english -- https://main.php.net/note/delete/126821/non-english
Del: in docs -- https://main.php.net/note/delete/126821/in+docs
Del: other reasons-- https://main.php.net/note/delete/126821
Reject -- https://main.php.net/note/reject/126821
Search -- https://main.php.net/manage/user-notes.php