【问题标题】:How to create a dynamically named method如何创建动态命名的方法
【发布时间】:2014-02-28 08:01:30
【问题描述】:

例如:

class Foo() {
    const BAR_FUNC = 'bar';
    //!!!the following function name needs to use self::BAR_FUNC rather than "hardcoded"     text "bar" in the function name
    function get_default_bar() {
        return 'this is my bar';
    }
}

http://au1.php.net/functions.variable-functions 有一个动态命名函数的例子,但它不适用于类方法:

创建并调用动态命名的函数

$tmp = "foo";
$$tmp = function() {
    global $tmp;
    echo $tmp;
}; 

$$tmp();

输出“foo”

【问题讨论】:

    标签: dynamic


    【解决方案1】:

    您给出的示例是基于动态名称调用函数,而不是基于动态名称创建函数。

    Dynamically Create Instance Method in PHP 已有答案

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 2022-12-14
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2013-08-24
      • 2016-08-28
      相关资源
      最近更新 更多