38

I'm trying to use call_user_func to call a method from another method of the same object, e.g.

class MyClass
{
    public function __construct()
    {
        $this->foo('bar');
    }
    public function foo($method)
    {
        return call_user_func(array($this, $method), 'Hello World');
    }

    public function bar($message)
    {
        echo $message;
    }
}

new MyClass; Should return 'Hello World'...

Does anyone know the correct way to achieve this?

Many thanks!

2
  • 1
    Looks about right. What doesn't work? Commented Nov 26, 2010 at 19:39
  • 1
    Return has defined meaning in PHP and if you are refering to something else, you should clarify that. Commented Nov 26, 2010 at 20:04

3 Answers 3

43

The code you posted should work just fine. An alternative would be to use "variable functions" like this:

public function foo($method)
{
     //safety first - you might not need this if the $method
     //parameter is tightly controlled....
     if (method_exists($this, $method))
     {
         return $this->$method('Hello World');
     }
     else
     {
         //oh dear - handle this situation in whatever way
         //is appropriate
         return null;
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

this is also faster than using call_user_func though I've seen many frown upon doing it this way... I am not one of those :-)
this is one of those 'with great power comes great responsibility' idioms...use it for good, not evil :)
I would slightly frown upon this because it throws a fatal error if the method doesn't exist, but it's probably safe to assume that check is done elsewhere
@pekka: well for me this is always wrapped in check for method_exists($obj, $method) or is_callable depending on the usage.
Thanks, I hadn't thought of that approach :)
23

This works for me:

<?php
class MyClass
{
    public function __construct()
    {
        $this->foo('bar');
    }
    public function foo($method)
    {
        return call_user_func(array($this, $method), 'Hello World');
    }

    public function bar($message)
    {
        echo $message;
    }
}

$mc = new MyClass();
?>

This gets printed out:

wraith:Downloads mwilliamson$ php userfunc_test.php 
    Hello World

1 Comment

Thank you, I will give it another go, I must of had an error when I originally tried it!
3

new MyClass; Should return 'Hello World'...

A constructor does not return anything.

6 Comments

I think "return" is meant in a very loose sense here (read: he means the echo). Still, you're right
@Pekka and whoever agreed with you. If the OP meant return to mean echo, then why is the OP asking the question? The code does echo when the ctor is invoked.
@Gordon the OP is 1) returning the results of a function that doesn't return anything, and 2) not attempting to return anything in the constructor. To me, that is overwhelming evidence that he didn't really mean "return" :)
@gordon: I agreed with Pekka. And while your interpretation is valid formt he point you mention i would say that Pekka's is as well since the OP doesnt even attempt a return $this->foo('bar') in his constructor :-)
@Pekka bar does not return anything, but that doesnt mean that other methods might not return something. foo is just invoking arbitrary methods so it's reasonable to have a return there.
|

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.