keenlearner Posted January 13, 2008 Share Posted January 13, 2008 I need to to a little functional programming, my question is in the code. Thanks. <?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'); ?> Can I have the array that has been passed to stay alive until passed to the print function ? Link to comment https://forums.phpfreaks.com/topic/85772-php-functional-programming/ Share on other sites More sharing options...
sKunKbad Posted January 13, 2008 Share Posted January 13, 2008 if you want to pass a value into a class, then the variable needs to be declared as public, and then on the outside you would use $obj->variableName = 'whatever'; is that what you are asking? Link to comment https://forums.phpfreaks.com/topic/85772-php-functional-programming/#findComment-437769 Share on other sites More sharing options...
keenlearner Posted January 13, 2008 Author Share Posted January 13, 2008 Thanks for the reply, but sorry that is not what I mean. In short, I want to get the function print2 executed. I need this line to be completed. But not sure how to implement that, thanks. if(is_array($arg1)) $code = "return $funcName(/*How should I write here for array ? Thanks*/ ,\$arg2)"; It's so difficult in PHP in contrast to Javascript. Link to comment https://forums.phpfreaks.com/topic/85772-php-functional-programming/#findComment-437773 Share on other sites More sharing options...
sKunKbad Posted January 13, 2008 Share Posted January 13, 2008 If you want to return something from the $funcName, then you have to return the result from inside the function, and then apply it to a variable for further use. Maybe I don't fully understand, but looking at your line of code, that was what came to mind. When I get stuck, I go back to basics. Check this out: <?php function getPath(){ $path = getcwd(); return $path; } echo "Your Absoluthe Path is: "; $whatever = getPath(); echo $whatever; ?> Link to comment https://forums.phpfreaks.com/topic/85772-php-functional-programming/#findComment-437790 Share on other sites More sharing options...
keenlearner Posted January 13, 2008 Author Share Posted January 13, 2008 I think my question is not very clear, but really appreciate of your help. I think maybe this is the solution. The key is the var_export Or any other advice for me ? Many thanks. <?php function print1($arg1, $arg2) { echo 'Print 1 function : '; var_dump($arg1); var_dump($arg2); echo '<br>'; } function print2($arg1,$arg2) { echo 'Print 2 function : '; var_dump($arg1); var_dump($arg2); echo '<br>'; } function applyOne($arg1,$funcName) { if(is_array($arg1)) { $str = var_export($arg1,true); $code = "return $funcName( $str ,\$arg2);"; } else $code = "return $funcName(\"$arg1\", \$arg2);"; return create_function('$arg2', $code); } $printFunction = applyOne('string 1', 'print1'); $printFunction('arg2 string'); class obj { public $var1; function __construct(){} function __set_state($array) { $obj = new obj; $obj->var1 = $array['var1']; return $obj; } } $obj = new obj(); $obj->var1 = 'variable 1'; $printFunction = applyOne(array($obj), 'print2'); $printFunction('arg2 string'); ?> Link to comment https://forums.phpfreaks.com/topic/85772-php-functional-programming/#findComment-437794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.