How to alias a function in PHP?

前端 未结 15 1058
死守一世寂寞
死守一世寂寞 2020-11-30 00:13

Is it possible to alias a function with a different name in PHP? Suppose we have a function with the name sleep. Is there a way to make an alias called wa

15条回答
  •  有刺的猬
    2020-11-30 00:52

    function alias($function)
    {
        return function (/* *args */) use ($function){
            return call_user_func_array( $function, func_get_args() );
        };
    }
    
    $uppercase = alias('strtoupper');
    $wait      = alias('sleep');
    
    echo $uppercase('hello!'); // -> 'HELLO!'
    
    $wait(1); // -> …
    

提交回复
热议问题