PHP syntax for dereferencing function result

后端 未结 22 1327
不知归路
不知归路 2020-11-22 02:14

Background

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring

22条回答
  •  生来不讨喜
    2020-11-22 02:43

    Extremely ghetto, but, it can be done using only PHP. This utilizes a lambda function (which were introduced in PHP 5.3). See and be amazed (and, ahem, terrified):

    function foo() {
        return array(
            'bar' => 'baz',
            'foo' => 'bar',
    }
    
    // prints 'baz'
    echo call_user_func_array(function($a,$k) { 
        return $a[$k]; 
    }, array(foo(),'bar'));
    

    The lengths we have to go through to do something so beautiful in most other languages.

    For the record, I do something similar to what Nolte does. Sorry if I made anyone's eyes bleed.

提交回复
热议问题