V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
moult
V2EX  ›  PHP

PHP 有没有花式定义函数的办法?

  •  
  •   moult · Jan 20, 2017 · 2174 views
    This topic created in 3389 days ago, the information mentioned may be changed or developed.
    $funcList = array(
        'foo' => function(){},
        'bar' => function(){}
    );
    foreach($funcList as $funcName=>$closure){
        // 循环将 funcList 里面的函数抽取到外面!
        // function $funcName{ $closure(); }
    }
    
    2 replies    2017-01-21 12:55:30 +08:00
    holyghost
        1
    holyghost  
       Jan 20, 2017
    ```
    <?php

    class a {
    protected $x = "x";

    public function xy() {
    $funcList = array(
    'foo' => function() { echo "a"; },
    'bar' => function() { echo "b"; }
    );

    foreach ($funcList as $funcName => $closure) {
    $closure();
    }
    }
    }

    $a = new a();
    $a->xy();
    ```


    或者


    ```
    <?php

    class b {}

    $obj = new b();
    $obj->yz = function () {
    return 'b';
    };
    echo($obj->yz->__invoke());
    ```

    不过。。。。有啥意义么
    claysec
        2
    claysec  
       Jan 21, 2017
    @holyghost 绕过 WAF -_-
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2505 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 02:38 · PVG 10:38 · LAX 19:38 · JFK 22:38
    ♥ Do have faith in what you're doing.