note 14322 modified in function.create-function by vrana

From: Date: Tue, 17 Aug 2004 14:55:55 +0000
Subject: note 14322 modified in function.create-function by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80050@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: <?php 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: <?php 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. --was-- 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://php.net/manual/en/function.create-function.php

« previous php.notes (#80050) next »