PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
function print1($arg1, $arg2)
{
echo 'Print 1 function : ';
var_dump($arg1);
var_dump($arg2);
}
function print2($arg1,$arg2)
{
echo 'Print 2 function : ';
var_dump($arg1);
var_dump($arg2);
}
function applyOne($arg1,$funcName)
{
if(is_array($arg1)) $code = "return $funcName(/*How should I write here for array ? Thanks*/ ,\$arg2)";
else $code = "return $funcName(\"$arg1\", \$arg2);";
return create_function('$arg2', $code);
}
//this is working
$printFunction = applyOne('string 1', 'print1');
$printFunction('arg2 string');
//'This is not working, how to make it work for array ? But note that the array may contain other type of objects, not just string. Thanks.
$printFunction = applyOne(array('string 1'), 'print2');
$printFunction('arg2 string');
?>
Last edited by keenlearner on Sat Jan 12, 2008 11:07 pm, edited 1 time in total.
Thanks for the prompt reply, but will it work if the elements inside the array is not string, but is other type of objects ?
Can I have the array that has been passed to stay alive until passed to the print function ?
Thanks, but I will get error of ...if I minus the slash. I put the slash because the $arg2 has not been set, so I don't want it to be parsed and it's going to be set only when the function has been created.
Parse error: syntax error, unexpected ')' in /var/www/test.php(29) : runtime-created function on line 6
keenlearner wrote:Thanks, but I will get error of ...if I minus the slash. I put the slash because the $arg2 has not been set, so I don't want it to be parsed and it's going to be set only when the function has been created.
Parse error: syntax error, unexpected ')' in /var/www/test.php(29) : runtime-created function on line 6
Many thanks.
I did not realize one could escape variables with slashes, I assumed only quotes would be escaped. Typically from my experience one would user single quotes if they did not wish for the variable to be parsed.