note 113150 added to function.create-function
| From: | ionix at NOSPAM dot post dot com | Date: | Thu, 05 Sep 2013 16:56:03 +0000 |
| Subject: | note 113150 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196175@lists.php.net to get a copy of this message | ||
I experimented a bit in creating a lambda function using a variable amount of arguments. While I
couldn't find an efficient way to create a random bit of code, I was able to get it to behave
as a front end for existing functions (as well as user-defined of course).
The code required me to use the dreaded eval() function to get it to use a variable amount of
parameters, so be sure that all input is thoroughly cleaned if derived from the user.
<?php
function run_function( $function )
{
// Get Arguments, Unset Exisitng Parameter
$params = func_get_args();
unset( $params[0] );
if( ( $count = count( $params ) ) > 0 )
{
$args = '$a';
$inc = 'b';
// Create Argument String - Formats as '$a, $b, $c' Per Number of Arguments
for( $x = 1; $x < $count; $x++ )
{
$args .= ', $' . $inc;
$inc++;
}
// Create Lambda Function and Format Paramters
$lambda = create_function( $args, 'return ' . $function . '(' . $args .
');' );
$params = "'" . implode('\', \'', $params) .
"'";
// Build and Evaluate Function with Parameters
$eval = '$return = $lambda(' . $params . ');';
eval($eval);
return $return;
}
else
{
return false;
}
}
?>
For most applications you would be better off calling the function normally. Perhaps this little
proof of concept may find some use if adapted to work by someone who requires it.
----
Server IP: 69.147.83.199
Probable Submitter: 75.31.207.158
----
Manual Page -- http://php.net/manual/en/function.create-function.php
Edit -- https://master.php.net/note/edit/113150
Del: integrated -- https://master.php.net/note/delete/113150/integrated
Del: useless -- https://master.php.net/note/delete/113150/useless
Del: bad code -- https://master.php.net/note/delete/113150/bad+code
Del: spam -- https://master.php.net/note/delete/113150/spam
Del: non-english -- https://master.php.net/note/delete/113150/non-english
Del: in docs -- https://master.php.net/note/delete/113150/in+docs
Del: other reasons-- https://master.php.net/note/delete/113150
Reject -- https://master.php.net/note/reject/113150
Search -- https://master.php.net/manage/user-notes.php