tags:

views:

91

answers:

5

How would I do something like this :

class Test
{

    public function test($methodName) {
     $this->$methodName;
    }

    private function a() {
     echo("a");
    }

    private function b() {
     echo("b");
    }

}

$testObj = new Test();
$testObj->test("a()");
$testObj->test("b()");

Maybe I should just pass a parameter "TYPE" and use a "IF statement" but I'm just curious! :)

And what if the "dynamic function name" has one or more parameters?

UPDATE : Thanks everyone! :)

UPDATE #2 - Answer :

class Test
{

    public function testOut($methodName) {
        $this->$methodName();
    }

    private function a() {
        echo("a");
    }

    private function b() {
        echo("b");
    }

}

$testObj = new Test();
$testObj->testOut("a");
$testObj->testOut("b");

The problem with the class is that there was a method named "Test" (the same as the class name)... I changed it and it worked.

+3  A: 
class Test
{

    public function test($methodName) {
        $this->$methodName();
    }

    private function a() {
        echo("a");
    }

    private function b() {
        echo("b");
    }

}

$testObj = new Test();
$testObj->test("a");
$testObj->test("b");
Itay Moav
Actually it gives me an error :Method name must be a string in ....:(
mrmuggles
The method name was the "constructor" because it was the same name as the class, so that's why it didn't work I think. I posted an update with your code, only changing the method "test" for "testOut"
mrmuggles
U R right, been some time since I used CPP,C# and other OOP languages where there is no __construct
Itay Moav
+1  A: 

Check out call_user_func() - it should do what you want.

Documentation here

inkedmn
+1  A: 

call_user_func allows you to do things like this.

For example,

funcname = 'a';
call_user_func(array($testObj, $funcname));

The other alternative is to use variable methods

For example,

$funcname = 'a';
$testObj->$funcname();
thomasrutter
+1  A: 

If you have a "dynamic function name" with more than one parameter, you can use call_user_func_array, like this:

//in class context..
//$parameters can be an array like array(1,2)
call_user_func_array(array($this, $methodName), $parameters);

The reason you would want to use call_user_func_array instead of call_user_func is that you can pass the parameters the function takes as an array, instead of just as additional parameters, thus allowing you to easily have a variable number of arguments.

Jani Hartikainen
A: 

Following your particular example and use case, the only way to achieve this exactly:

$testObj = new Test();
$testObj->test("a()");
$testObj->test("b()");

would be to use eval():

class Test
{

    public function test($methodName) {
        eval($methodName);
    }

    private function a() {
        echo("a");
    }

    private function b() {
        echo("b");
    }

}

$testObj = new Test();
$testObj->test("a()");
$testObj->test("b()");

Others pointed out a solution using call_user_func() and rightly so. Also evidently, is faster than using eval() for function calls. Read this:

http://nz.php.net/manual/en/function.call-user-func.php#64415

karim79
Why does pointing out another way to do something, with an example and some good reading get an immediate downvote without explanation?
karim79
I don't know, it was useful to me. thanks ;)
mrmuggles