2

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.

5 Answers 5

4
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");
Sign up to request clarification or add additional context in comments.

2 Comments

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"
U R right, been some time since I used CPP,C# and other OOP languages where there is no __construct
1

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

Documentation here

Comments

1

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();

Comments

1

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.

Comments

0

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

1 Comment

Why does pointing out another way to do something, with an example and some good reading get an immediate downvote without explanation?

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.