crist
V2EX  ›  PHP

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

  •  
  •   crist · Jul 14, 2016 · 5616 views
    This topic created in 3596 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
        3
    rekulas  
       Jul 14, 2016   ❤️ 1
    return $this->{$funcname}();
    wh1012023498
        4
    wh1012023498  
       Jul 14, 2016
    回复支持 md 吗。。

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

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

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