如何从具有变量的类调用方法?

时间:2020-12-07 22:22:18

Given this class:

鉴于此课程:

class Tacobell{

    public function order_taco(){
        echo "3 Tacos, thank you.";
    }

    public function order_burrito(){
        echo "Cheesy bean and rice, please";
    }

}

$lunch = new Tacobell;
$lunch->order_burrito();
$lunch->order_taco();

How would I do something like this?

我该怎么办?

$myOrder = 'burrito';
$lunch->order_.$myOrder;

Obviously that code is bunk--but shows what I'm attempting to do better than trying to explain it away.

显然,代码是无聊的 - 但是显示我试图做的比试图解释它更好。

And maybe I'm going about this all wrong. I thought about a method with a switch statement, pass in burrito or taco, then call the right method from there. But then I have to know the end from the beginning, and I may potentially have lots of methods and I'd rather not have to update the switch statement everytime.

也许我说这一切都错了。我想到了一个带有switch语句的方法,传入burrito或taco,然后从那里调用正确的方法。但是我必须从头开始知道结束,我可能有很多方法,而且我不必每次都更新switch语句。

Thanks!

4 个解决方案

#1


How about something like this?

这样的事怎么样?

class Tacobell {
    public function order_burrito() {
         echo "Bladibla.\n";
    }

    public function order($item) {
        if (method_exists($this, "order_$item")) {
            $this->{'order_' . $item}();
        } else {
            echo "Go away, we don't serve $item here.\n";
        }
    }
}

You would call it using $lunch->order('burrito');, which looks much cleaner to me. It puts all the uglyness in the method Tacobell::order.

你可以用$ lunch-> order('burrito')来调用它,这对我来说看起来更干净。它把所有的丑陋都放在方法Tacobell :: order中。

#2


$lunch->{'order_' . $myOrder}();

I do agree the design is a little iffy, but that's how to do it at least.

我同意这个设计有点不确定,但这至少是如何做到的。

#3


I think call_user_func is what you're looking for:

我认为call_user_func是你正在寻找的:

http://us3.php.net/call_user_func

You can pass it the string you suggested. See example #3 for calling a method of a class.

你可以传递你建议的字符串。请参阅示例#3以调用类的方法。

#4


simple enough

$order = 'order_burrito';
$lunch->$order();

#1


How about something like this?

这样的事怎么样?

class Tacobell {
    public function order_burrito() {
         echo "Bladibla.\n";
    }

    public function order($item) {
        if (method_exists($this, "order_$item")) {
            $this->{'order_' . $item}();
        } else {
            echo "Go away, we don't serve $item here.\n";
        }
    }
}

You would call it using $lunch->order('burrito');, which looks much cleaner to me. It puts all the uglyness in the method Tacobell::order.

你可以用$ lunch-> order('burrito')来调用它,这对我来说看起来更干净。它把所有的丑陋都放在方法Tacobell :: order中。

#2


$lunch->{'order_' . $myOrder}();

I do agree the design is a little iffy, but that's how to do it at least.

我同意这个设计有点不确定,但这至少是如何做到的。

#3


I think call_user_func is what you're looking for:

我认为call_user_func是你正在寻找的:

http://us3.php.net/call_user_func

You can pass it the string you suggested. See example #3 for calling a method of a class.

你可以传递你建议的字符串。请参阅示例#3以调用类的方法。

#4


simple enough

$order = 'order_burrito';
$lunch->$order();