Php call method by string

Class MyClass{
  private $data=array('action'=>'insert');
  public function insert(){
    echo 'called insert';
  }

  public function run(){
    $this->$this->data['action']();
  }
}

This doesn't work:

$this->$this->data['action']();

Is the only possibility to use call_user_func();?

Melebius

5,8504 gold badges35 silver badges49 bronze badges

asked Mar 27, 2011 at 18:37

Php call method by string

Try:

$this->{$this->data['action']}();

You can do it safely by checking if it is callable first:

$action = $this->data['action'];
if(is_callable(array($this, $action))){
    $this->$action();
}else{
    $this->default(); //or some kind of error message
}

answered Mar 27, 2011 at 18:38

Mārtiņš BriedisMārtiņš Briedis

17.1k5 gold badges53 silver badges74 bronze badges

5

Reemphasizing what the OP mentioned, call_user_func() and call_user_func_array() are also good options. In particular, call_user_func_array() does a better job at passing parameters when the list of parameters might be different for each function.

call_user_func_array(
    array($this, $this->data['action']),
    $params
);

answered Oct 8, 2012 at 16:27

Brad KochBrad Koch

18.3k18 gold badges107 silver badges135 bronze badges

  1. HowTo
  2. PHP Howtos
  3. Call Function From String in PHP

Created: February-23, 2022

  1. Use call_user_func() Call a Function From a String in PHP
  2. Use the Variable Method to Call a Function From a String Stored in a Variable in PHP

PHP has a built-in function like call_user_func() to call a function from a string or store it in a variable for the older versions. PHP can also store a function into a variable and use it wherever required.

This tutorial demonstrates different methods to call a function from a string stored in a variable.

Use call_user_func() Call a Function From a String in PHP

PHP built-in function call_user_func() can be used for the purpose.

Example:

";
}
?>

The code above calls the function demo_func with the parameter name.

Output:

Hello This is Delftstack employee John
Hello This is Delftstack employee Shawn
Hello This is Delftstack employee Michelle
Hello This is Delftstack employee Tina

Use the Variable Method to Call a Function From a String Stored in a Variable in PHP

In PHP, we can also store the functions in variables. The function name should be assigned to a variable as a string, and then we can call the function a variable.

Example:

";
}
?>

The output for this will also be similar to our first example above.

Hello This is Delftstack employee John
Hello This is Delftstack employee Shawn
Hello This is Delftstack employee Michelle
Hello This is Delftstack employee Tina

call_user_func is the old way to do it. We can directly store the function into variables as a string and call the function by calling the variable.

Related Article - PHP Function

  • Create a PHP Function With Multiple Returns
  • Properly Format a Number With Leading Zeros in PHP
  • Use ODBC Functions in PHP
  • Use the sleep() Function in PHP
  • Php call method by string

    How do you call a method in a string?

    There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

    How do you call a function inside a string in PHP?

    PHP has a built-in function like call_user_func() to call a function from a string or store it in a variable for the older versions. PHP can also store a function into a variable and use it wherever required.

    How do you call a method in PHP?

    To invoke a method on an object, you simply call the object name followed by "->" and then call the method. Since it's a statement, you close it with a semicolon. When you are dealing with objects in PHP, the "->" is almost always used to access that object, whether it's a property or to call a method.

    How do you call a method with a variable name?

    You could try something like: Method m = obj. getClass(). getMethod("methodName" + MyVar); m.