I need to to a little functional programming, my question is in the code. Thanks.
[code]<?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.
class obj {
function __construct(){}
}
$obj = new obj();
$printFunction = applyOne(array($obj), ‘print2’);
$printFunction(‘arg2 string’);
?>[/code]
Can I have the array that has been passed to stay alive until passed to the print function ?