1

I would like to have an array of methods in my php class, indexed with method names, so that I can do something like this:

public function executeMethod($methodName){
 $method=$this->methodArray[$methodName];
 $this->$method();
 // or some other way to call a method whose name is stored in variable $methodName
}

I've found this for __call:

The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope

However, methods I'd like to use in executeMethod are visible.

What is proper way to do that? Is it possible?

EDIT: I wanted to get a method name in the executeMethod, and then call the method of the given name, and had an idea of methods array.

5
  • So what's your question? Commented Mar 28, 2013 at 16:37
  • What exactly isn't working? Commented Mar 28, 2013 at 16:38
  • Can you show what $this->methodArray contents look like? How does the value differ from the key? Commented Mar 28, 2013 at 16:40
  • I didn't know how to call methods, but thanks to answers below, I figured it out. I wanted something like this: $methodArray = (methodName=>method), but having read that I can call methods by using string, I made it work. : ) Commented Mar 28, 2013 at 19:11
  • See also stackoverflow.com/questions/45458840. This question addresses the former title of this question "how to get methods into an array". The answers in this post never addressed this part. Commented May 1, 2018 at 18:35

3 Answers 3

1

you can call object methods and properties by using string with syntax

$method = 'your_method_name_as_string';
$this->$method();

from php doc

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I didn't know that I can call methods with string variables. Now I don't need an array of methods. You helped me a lot! : )
0

Maybe you are looking for something like this:

public function executeMethod($methodName) {
    if (isset($this->methodArray[$methodName])) {
        $method = $this->methodArray[$methodName];
        return call_user_func(array($this, $method));
    }
    throw new Exception("There is no such method!");
}

Comments

0

anonymous functions are available in php 5.3

i think you're trying to do something like

$tmp['doo'] = function() { echo "DOO"; };
$tmp['foo'] = function() { echo "FOO"; };
$tmp['goo'] = function() { echo "GOO"; };

$tmp['doo']();

Comments

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.