du21064 2018-01-18 08:50
浏览 157
已采纳

在PHP中有一种方法来对函数/方法调用进行别名吗?

I'm not talking about aliasing the actual function/method name itself but aliasing their calls:

$test->some_method($a,$b);

Can we alias the above as something like:

$t_sm($a,$b);

Again, I'm not talking about aliasing the actual function/method name, but rather their actual calls, minus the parameters.

EDIT

And no, I don't want to create wrapper functions. And strictly looking at readability purpose here. It's much more readable that way.

EDIT

When aliasing the actual method call it includes the name of the object including the method it's calling. Where as if I aliased just the method, i would still have to call the object on it explicitly.

EDIT Is there a method call aliasing such that:

class Foo
{   
    function Bar($msg)
    {
        echo $msg;
    }
}

$f_b = 'Foo::Bar';
$msg = "calling method Bar via method-call-aliasing.";
$f_b($msg);

It creates instance of Foo and aliases that instance as well as the method specified('Bar' part of string 'Foo::Bar').

  • 写回答

2条回答 默认 最新

  • dra87370 2018-01-18 08:53
    关注

    You need to create a callable pseudo-type, which for an object method is an array such as:

    $t_sm = [$test, 'some_method'];
    

    This is then callable as:

    $t_sm($a, $b);
    

    Further documentation: http://php.net/manual/en/functions.variable-functions.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?