0

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').

webcdr
  • 21
  • 5

2 Answers2

5

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

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Very nice, tyvm. – webcdr Jan 18 '18 at 08:59
  • Actually can you post the callable pseudo-type implementation? – webcdr Jan 18 '18 at 09:15
  • What do you mean? I've linked to the documentation, which is all I can provide. – deceze Jan 18 '18 at 09:16
  • All their solutions showed use of 'call_user_func' function...I don't want to use that function, but rather only $t_sm($a,$b) on its own... – webcdr Jan 18 '18 at 09:18
  • Yeah, `call_user_func` is not really necessary here. Any `callable` can be called with the `$c()` syntax. This mostly came out of support for anonymous functions if I remember correclty, e.g. `$c = function () {}`. At least since then all `callable`s are callable directly, since it's not always possible to know how a `callable` was created and hence it makes no sense to differentiate how they can be called. – deceze Jan 18 '18 at 09:24
  • I've added another link to more relevant documentation. – deceze Jan 18 '18 at 09:28
  • in the method example you still have to explicitly call the object on the method...I want to create an alias the method AND the object calling it. Please see my second EDIT. – webcdr Jan 18 '18 at 09:34
  • Unclear what you mean. `$t_sm` is an alias to the method `some_method` of the object `$test`. You don't need to worry about `$test` when calling `$t_sm`. – deceze Jan 18 '18 at 09:36
  • Atleast from the examples in the link u last provided...yes you do. Please see my third edit. – webcdr Jan 18 '18 at 09:43
  • actually no your right, it's in example 4. K'll ill check off, thanks. – webcdr Jan 18 '18 at 09:45
0

Hi bro i think your looking for this. it has been questioned before:

<?php
 namespace Name\Space {
    const FOO = 42;
   function f() { echo __FUNCTION__."\n"; }
}

namespace {
    use const Name\Space\FOO;
     use function Name\Space\f;

    echo FOO."\n";
    f();
}
?>

OUTPUT: 42

Name\Space\f

reference:http://docs.php.net/manual/en/migration56.new-features.php#migration56.new-features.use

Can we alias a function in php?

Dee
  • 166
  • 5