1

I have a problem in calling a function that the name is being stored in an array.

class tempClass {
   function someFunction() {
      $tempArray = array('functionName' => 'tempFunction');
      $tempArray['functionName']();
   }

   function tempFunction() {
      echo "inside function";
   }
}

It gives me an error:

"Fatal error: Call to undefined function tempFunction() in /..... line..".

Line number is the line where the function is being called, $tempArray['functionName']();

But if called the method_exists(), it shows that the method is exists. It is very confusing. Can anyone please help me out? Thanks.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Abhishek Salian
  • 928
  • 2
  • 10
  • 27
  • possible duplicate of [Can you store a function in a PHP array?](http://stackoverflow.com/questions/1499862/can-you-store-a-function-in-a-php-array) – Donal Fellows Nov 25 '12 at 15:34

2 Answers2

4

Use call_user_func() , like this:

call_user_func($tempArray['functionName']);

UPDATE:
As you want to call a method of a class from inside that class, use the following instead:

call_user_func(array($this, $tempArray['functionName']));

See working demo

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • I got this error "Warning: call_user_func() expects parameter 1 to be a valid callback, function 'tempFunction' not found or invalid function name in...." – Abhishek Salian Nov 25 '12 at 13:27
  • 1
    Same problem as with your original attempt: you are missing the scope. – arkascha Nov 25 '12 at 13:28
  • yes you are right, How stupid of me. I used $this->$tempArray['functionName'](); and it worked. Thanks a lot guys. – Abhishek Salian Nov 25 '12 at 13:33
  • @AbhishekSalian I've updated my answer with a corrected version for your case (calling a method of the class from within the class), check it out. – Nelson Nov 25 '12 at 13:34
  • @arkascha Yes I didn't notice it first time, check out my update for the fixed version. – Nelson Nov 25 '12 at 13:36
  • @arkascha Because your answer is not working, as OP told you, you'd notice if you actually run it, see the error here http://codepad.org/CIT7IDhT – Nelson Nov 25 '12 at 13:51
  • @Nelson: you are right, sorry for the confusion. I update my answer accordingly, yours is the better answer! – arkascha Nov 25 '12 at 13:59
2

Well you ask if the method exists inside the class or object, but you call it without that scope. That won't work...

Try this instead:

call_user_method($tempArray['functionName'],$this);

Just saw that call_user_method() is depreciated. Use call_user_func() as answered by Nelson instead.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • it didnt work with the above cmd but i just added the scope to the calling function. thanks. – Abhishek Salian Nov 25 '12 at 13:34
  • This answer does not work, the error can be seen here http://codepad.org/CIT7IDhT – Nelson Nov 25 '12 at 13:52
  • @Nelson: that is true, sorry for that. I corrected the answer, but Nelsons answer is better: just saw that `call_user_mothod` is depreciated! Sorry for the confusion. – arkascha Nov 25 '12 at 13:57