Use Variable as Function Name in PHP

PHP: Variable in a function name

You can do that, but not without interpolating the string first:

$animfunc = 'sound_' . $animal;
print $animfunc();

Or, skip the temporary variable with call_user_func():

call_user_func('sound_' . $animal);

Use Variable as Function Name in PHP

Declaring a function with a variable but arbitrary name like this is not possible without getting your hands dirty with eval() or include().

I think based on what you're trying to do, you'll want to store an anonymous function in that variable instead (use create_function() if you're not on PHP 5.3+):

$variableA = function() {
// Do stuff
};

You can still call it as you would any variable function, like so:

$variableA();

How to call a function from a string stored in a variable?

$functionName() or call_user_func($functionName)

to use variable in function name in PHP

After reading your question and criticism on my answer. I think you're just looking for an OOP implementation

class Person {
public function __construct() // init()
{
/* Do something */
}

public function setup()
{
/* Do something */
}

/* etc */
}

And use it as follows:

$john = new Person(); // __construct() will be executed here automaticaly
$john->setup();

See PHP documentation for more information about Classes & Objects in PHP: http://www.php.net/manual/fa/classobj.examples.php

Call a variable function using a class property as function name

You need to implement a magic __call method, like this:

class C
{
private $prop = "execute";

public function __call($method, $args)
{
if($method == "prop") // just for this prop name
{
if(method_exists($this, $this->prop))
return call_user_func_array([$this, $this->prop], $args);
}
}

public function execute ($s){
echo '>>'.$s.'<<';
}

}

$c = new C;
$c->prop(123);

loop variable and function names php

you can change variable name using this function:

   function get_var_name($var) {
foreach($GLOBALS as $var_name => $value) {
if ($value === $var) {
return $var_name;
}
}

return false;
}

and if you want to change variable name $request with $request1 you can do:

${get_var_name($request).'1'} = $request;

and if you want to create function dynamically:

  $function = create_function("/* comma separated arguments*/", "/*code as string*/);

and call it:

$function();

PHP: define functions with variable names

If you really had to you could declare the function within an eval block:

foreach ($a as $functionname)
eval('
function '.$functionname.' () {
print 123;
}
');

But that incurs some extra parsing time speed penalty over just declaring the functions in a file.

Pass a function name as parameter in a function

function test($str, $func){
return call_user_func($func,$str);
}

Use it like this

$asd = test("ASD","strtolower");

How to use variable function-name in a loop?

Although this isn't exactly the greatest design, you could use call_user_func and an array containing the method you want to call. In your case, you could do something like:

call_user_func(array($menuSet, 'GetMenuLink' . $i));

which uses PHP's callable arrays.

Personally, however, I would recommend refactoring this. Do this processing in the class, in a method named GetMenuLinks or similar.



Related Topics



Leave a reply



Submit