V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
crist
V2EX  ›  PHP

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

  •  
  •   crist · 2016-07-14 18:19:38 +08:00 · 4653 次点击
    这是一个创建于 2814 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2016-07-16 08:29:22 +08:00
    g079708
        2
    g079708  
       2016-07-14 18:57:00 +08:00
    rekulas
        3
    rekulas  
       2016-07-14 21:57:14 +08:00   ❤️ 1
    return $this->{$funcname}();
    wh1012023498
        4
    wh1012023498  
       2016-07-14 22:49:37 +08:00
    回复支持 md 吗。。

    ```php
    echo 'hello meizi~';
    ```
    wh1012023498
        5
    wh1012023498  
       2016-07-14 22:50:23 +08:00
    = = 好绝望
    UnisandK
        6
    UnisandK  
       2016-07-14 23:02:39 +08:00
    @wh1012023498 支持 gist
    msg7086
        7
    msg7086  
       2016-07-15 06:53:07 +08:00
    is unexist -> does not exist.

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

    https://gist.github.com
    techmoe
        20
    techmoe  
       2016-07-15 23:03:23 +08:00 via Android
    可变函数后面得加括号吧。。
    realpg
        21
    realpg  
       2016-07-16 08:29:22 +08:00
    @crist 想缩写请用 not exist 大家都能懂 非得加上 is 啥的 看惯了英语的肯定觉得不伦不类
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.