3
<?php
  $str = "getList";

  //now by doing something to $str i need to call getList() method any sugesstions 

  function getList(){
    echo "get list called";
  }
?>

3 Answers 3

6

Use the call_user_func() function to call the function by name.

Sign up to request clarification or add additional context in comments.

Comments

6

This feature is known as Variable functions, here is an example from php.net:

 <?php
 function foo() {
     echo "In foo()<br />\n";
 }

 function bar($arg = '')
 {
     echo "In bar(); argument was '$arg'.<br />\n";
 }

 // This is a wrapper function around echo
 function echoit($string)
 {
     echo $string;
 }

 $func = 'foo';
 $func();        // This calls foo()

 $func = 'bar';
 $func('test');  // This calls bar()

 $func = 'echoit';
 $func('test');  // This calls echoit()
 ?>

More Info:

Comments

1

You can use the variable as a function name. This will execute getList():

$str();

However, stuff like this is mostly a symptom of a design problem. Care to elaborate what you need this for?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.