How to Alias a Function in PHP

How to alias a function in PHP?

PHP 5.6+ only

Starting with PHP 5.6 it is possible to alias a function by importing it:

use function sleep as wait;

There's also an example in the documentation (see "aliasing a function").

In PHP is there a way to alias function/method calls?

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

how to create an alias function in php

What you saw was an Interface. Their purpose is to create sort of a map of a class so you know that any class that implements it will have at least the required methods.

interface myInterface(){
// "alias functions"
public function foo();
public function boo();
}

class foo implements myInterface{
// "real functions"
public function foo(){}
public function boo(){}
}

phpDoc might be closer to what you're looking for. This is a sort of comment syntax that most IDE's use to read and autocomplete methods in your classes. In Netbeans for example, your IDE will offer a method description with parameters and whatever other info you provide. Here's an example from EasyImage source code:

/**
* Add perspective to an image
* @param float $gradient - gradient of perspective
* @param type $rightdown - angle
* @return \EasyImage
*/
public function perspective($gradient = 0.85, $rightdown="top"){ ... }

Using this function in Netbeans, showing function description and parameters:

Autocomplete in Netbeans

php - can't use as alias for functions with same name

In addition to @MEriksson comment you have to rewrite your code like that that you avoid functionname conflict. like that:

abstract example code

<?php

trait UserTrait {
public function getRoles() {
echo 'roleFromUserTrait';
}

public function make1() {
echo 'A';
}
}

trait PermissionResourcesAsRolesTrait {
public function getRoles() {
echo 'roleFromPermissionResourcesAsRolesTrait';
}
public function make2() {
echo 'B';
}
}

class Hello {
use UserTrait, PermissionResourcesAsRolesTrait {
UserTrait::getRoles insteadof PermissionResourcesAsRolesTrait;
UserTrait::getRoles as getR1;
PermissionResourcesAsRolesTrait::getRoles insteadof UserTrait;
PermissionResourcesAsRolesTrait::getRoles as getR2;
}
}

$h = new Hello();
print_r($h->getR1());
print_r($h->getR2());

import and alias a static method from a class

Aliasing doesn't magically convert methods into functions, try this instead

<?php
use \App\Helpers\HTTP as extract_path;
echo extract_path::extract_path_from_url( 'http://example.com/test' );

Also (it should go without saying) when you alias this only affects the namespace and class name, not methods of the class. These are generally used for 1 of 2 things. Resolving naming conflicts

 use NamespaceOne\Someclass;
use NamespaceTwo\Someclass as SecondClass;

If these were both put without an alias then using

 Someclass::method()

Would be ambiguous.

The second thing they can be used for is if you need a lot of classes imported from one namespace. Such as this:

use App\Exceptions\NoFile;
use App\Exceptions\FileSize;
use App\Exceptions\FileType;

throw new NoFile();
throw new FileSize();
throw new FileType();

Can be done this way:

use App\Exceptions as E;

throw new E\NoFile();
throw new E\FileSize();
throw new E\FileType();

Which is not only shorter, but easier to maintain if you change the namespace you have to only change it for the alias and then all is good. So in short it's not really intended for what you want to use it for.

Wrap it

You can always make a wrapper for it:

if(!function_exists('extract_path_from_url')){
function extract_path_from_url($url){
return \App\Helpers\HTTP::extract_path_from_url($url);
}
}

And then call it to your hearts content. Performance wise you do have an extra call by wrapping it, but generally wrappers make it easier to maintain. For example if you rename that method or class, you can change it in the wrapper and everything is good. So there is an argument to be made for either option.

You don't have to check if the function exists, but depending on how your overall system works it may not be a bad idea, so I included it in the example just for the sake of completeness. Personally in a case like this, I don't see any issue putting it right in the same file with the class, just remember to load it. If you are using autoloading the functions won't be included unless you manually load the file or otherwise force it to autoload. Assuming nothing else uses the class first, of course.

One method I have used in the past that I really like, is to make a file named http_functions (classname + _functions) and then add a static method to the class that registers the functions:

  class HTTP {
public static function regester_fuctions(){
require 'http_functions.php'
}
}

Then when you call HTTP::regester_fuctions() it autoloads HTTP class and includes all the functional wrappers. In fact I do this very thing in my really super awesome debug print class (queue shameless plug) https://github.com/ArtisticPhoenix/Debug

Just some thoughts, enjoy!

Can php call a static alias?

You're asking a lot of PHP here. It's supposed to know that your use statement is going to impact something in a completely different class, in a completely different file, just because?

If PHP did that by default it would cause a lot of very strange problems for people. Re-define the method, or as you point out, store that property in the class itself.

I think a lot of developers would expect, or at least prefer that PHP behave the way it does.

It sounds like what you need here is a factory function that can be redefined in the subclass to behave differently, or in other words, that getNewFoo() should be overridden in the subclass to use the alternate version.



Related Topics



Leave a reply



Submit