note 14322 added to function.create-function.php
| From: | ben-php at bacarisse dot btinternet dot co dot uk | Date: | Thu, 26 Jul 2001 17:33:49 +0000 |
| Subject: | note 14322 added to function.create-function.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15875@lists.php.net to get a copy of this message | ||
A nice technique for building complex search patterns on lists, files or whatever is to build
function combining functions like this:
function _not_($f) {
return create_function('$x',
"return !$f(\$x);");
}
function _and_($f, $g) {
return create_function('$x',
"return $f(\$x) && $g(\$x);");
}
(similarly for _or_ and others...). Once you've built your matching primitives you can then
build more complex matches into your script.
Unfortunately, as explained in (closed) bug #10721, the function names returned by create_function
have a null byte at the front and this causes a parse error.
You can fix the definition like this:
function _not_($f) {
$f = substr($f, 1);
return create_function('$x',
"return !call_user_func(chr(0).'$f', \$x)");
}
The expression that re-builds the function name avoid the null being literally in the parsed string.
If there is a better fix, please let me know.
http://www.php.net/manual/en/function.create-function.php