Calling function from its name in a PHP class

Asked

Viewed 982 times

6

I am creating an application where I need to call the function based on its name within the class. the intention to use this way is that I can call these functions through ajax and the server will understand without it being necessary to make a switch or something like that. Basically what I have is this

class MinhaClasse {
    var foo = 'ola mundo';
    var bar = 'teste para o stack';
    public function index($fn = 'a')
    {
        $result = $this->$fn(); // <-- é isso que eu quero
    }
    private function a()
    {
        return $this->foo;
    }
    private function b()
    {
        return $this->bar;
    }
}

Basically, I need that from a string or some element I send to the function index($fn) it performs a function within my class. Someone would have some way to do this?

EDIT: Today, in order to achieve this, I am obliged to make a switch, which pollutes the code and still requires me to keep editing if there are changes, as in the example below:

class MinhaClasse { //com switch tosco...
    var foo = 'ola mundo';
    var bar = 'teste para o stack';
    public function index($fn = 'a')
    {
        switch($fn){
            case 'b': 
               $result = $this->b();
            break;
            default : 
               $result = $this->a();
            break;
        };

        echo $result;
    }
    private function a()
    {
        return $this->foo;
    }
    private function b()
    {
        return $this->bar;
    }
}

2 answers

6


You can call the function using {}

Would look like this:

class MinhaClasse {
    var foo = 'ola mundo';
    var bar = 'teste para o stack';
    public function index($fn = 'a')
    {
        $result = $this->{$fn}(); // <-- é isso que eu quero
    }
    private function a()
    {
        return $this->foo;
    }
    private function b()
    {
        return $this->bar;
    }
}
  • 1

    that’s right, solved my problem!

  • 2

    I liked the {$fn}. It is a little known syntax, but it helps to avoid different interpretations especially now with the changes in PHP 7.

5

Can do this:

class MinhaClasse {
    var $foo = 'ola mundo';
    var $bar = 'teste para o stack';
    public function index($fn = a) {
        $result = $this->$fn(); // <-- é isso que eu quero
    }
    private function a() {
        return $this->foo;
    }
    private function b() {
        return $this->bar;
    }
}
$classe = new MinhaClasse();
$classe->index();
$classe->index(b);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I prefer this way by not using strings, you pass the function symbol itself.

  • 1

    It actually makes more sense. The other solution is equal per variable inside a string, like $a = "$parametro";, only makes the PHP interpreter process one more thing to get to the same thing your code does. I just don’t understand how you got -2 votes on your answer, kind of abnormal. Either it’s people who have no idea how PHP works, or it’s really dirty.

  • I removed my downvote, I went to read about the downvote and saw that it is in case the answer is wrong and/or dangerously wrong. Excuse my mistake, The opposite of what they say I’m not here to disrupt . But I believe that your answer is still not the best for the lack of typing, with string is better to work, but it is the 2 ways.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.