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条)

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败