crist
V2EX  ›  PHP

PHP 的类内如何使用动态函数名?

  •  
  •   crist · Jul 14, 2016 · 6003 views
    This topic created in 3763 days ago, the information mentioned may be changed or developed.

    class fmOutput { public function sex($value = '') { return 66; } public function op($funcname='', $value = '') { return $this->$funcname; }

    public function __call($funcName,$args)
    {
    	return "Oops,{{$funcName}}is unexist";
    }
    

    } $FM = new fmOutput('sex'); echo $FM->op();

    然后,没有任何反应,求各位大神解疑。。

    21 replies  •  2016-07-16 08:29:22 +08:00
    rekulas
    rekulas
    Jul 14, 2016❤️1
    3
    return $this->{$funcname}();
    wh1012023498
    wh1012023498
    Jul 14, 2016
    4
    回复支持 md 吗。。

    ```php
    echo 'hello meizi~';
    ```
    wh1012023498
    wh1012023498
    Jul 14, 2016
    5
    = = 好绝望
    UnisandK
    UnisandK
    Jul 14, 2016
    6
    @wh1012023498 支持 gist
    msg7086
    msg7086
    Jul 15, 2016
    7
    is unexist -> does not exist.

    unexist 是什么鬼。
    rekulas
    rekulas
    Jul 15, 2016
    8
    @wh1012023498
    ```
    php
    echo 'hello meizi~';
    ```后面不能跟其他字符
    ```
    rekulas
    rekulas
    Jul 15, 2016
    9
    ```
    ee
    ```
    rekulas
    rekulas
    Jul 15, 2016
    10
    ```
    代码模式好像不见了 = =
    ```
    crist
    crist
    OP
    Jul 15, 2016
    11
    @rekulas 这个是正确的,不用加大括号也可以
    crist
    crist
    OP
    Jul 15, 2016
    12
    @msg7086 does not exist 的缩写,你都知道是什么意思啦
    msg7086
    msg7086
    Jul 15, 2016
    13
    @rekulas 回复哪来的代码模式?
    rekulas
    rekulas
    Jul 15, 2016
    14
    @msg7086 之前貌似是可以的 我看到他们回复过代码
    rekulas
    rekulas
    Jul 15, 2016
    15
    @crist 主要是为了规范下 便于理清逻辑 有时候参数不一定是$xx 还有可能是$this->xx 之类的。。
    kchum
    kchum
    Jul 15, 2016
    16
    @rekulas 回复不支持 Markdown
    tanteng
    tanteng
    Jul 15, 2016
    17
    $this->{'register'.$command.'Command'}();
    rekulas
    rekulas
    Jul 15, 2016
    18
    **gggggggg**
    _ggg_
    ### h1h1
    hyq
    hyq
    Jul 15, 2016
    19
    那是 github 的 gist

    https://gist.github.com
    techmoe
    techmoe
    Jul 15, 2016 via Android
    20
    可变函数后面得加括号吧。。
    realpg
    realpg
    Jul 16, 2016
    21
    @crist 想缩写请用 not exist 大家都能懂 非得加上 is 啥的 看惯了英语的肯定觉得不伦不类
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1775 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 23:59 · PVG 07:59 · LAX 15:59 · JFK 18:59
    ♥ Do have faith in what you're doing.