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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建