Tested for PHP 5.3
As I see here, an anonymous function can help you: http://php.net/manual/en/functions.anonymous.php
What you may need, and he did not say before that how to pass a function without its wrapper inside the built-in function . As you will see later, you need to pass the name of the function written in the string as a parameter, check its "callability" and then call it.
Verification Function:
if( is_callable( $string_function_name ) ){ }
Then, to call it, use this piece of code (if you need parameters, put them in an array), look at: http://php.net/manual/en/function.call-user-func.php
call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters );
as follows (similar, without parameters):
function funToBeCalled(){ print("----------------------i'm here"); } function wrapCaller($fun){ if( is_callable($fun)){ print("called"); call_user_func($fun); }else{ print($fun." not called"); } } wrapCaller("funToBeCalled"); wrapCaller("cannot call me");
Here's a class explaining how to do something like this:
<?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->functions[$name] = $data; else $this->vars[$name] = $data; } function __get($name){ $t = $this->vars[$name]; if(isset($t)) return $t; else{ $t = $this->$functions[$name]; if( isset($t)) return $t; } } function __call($method,$args=null){ $fun = $this->functions[$method]; if(isset($fun)){ call_user_func_array($fun,$args); } else {
and usage example
<?php function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; $obj->justPrintSomething = 'sayHello'; $obj->justPrintSomething(); $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; $obj->justPrintSomething("Jack Sparrow"); $obj->justPrintSomething( $obj->justPrintSomething ); ?>
Marco Ottina Nov 30 '16 at 0:38 2016-11-30 00:38
source share