Is it possible to create a "synonym" of one function, two names for the same function?

3

I have a set of functions using a third-party API. From time to time I thought of making it available, since there is no public library in PHP and it makes no difference to keep it as a closed code or not. ( I will still have to change a lot. )

However, since I only use such functions, they have strange names that do not match the actual result, in addition to having no prefix. To solve this I thought of simply renaming the functions and include a prefix.

In this way all functions would change to:

MinhaApi_*

But that would be too long to write, so I thought I'd shorten it to:

ma_*

From there came the curiosity:

Is it possible to have MinhaApi_* and ma_* simultaneously, without declaring them twice?

So you can understand, instead of using this:

function MinhaApi_text(){
   return 'Isso é um texto';
}

function ma_text(){
   return MinhaApi_text();
}

echo ma_text();

Resultado: Isso é um texto
  

This works, but it requires you to "re-declare" all functions (so I guess that's not the best way).

Use this:

function MinhaApi_text(), ma_text(){
      return 'Isso é um texto';
}

echo ma_text();

Resultado "esperado": Isso é um texto
  

That does not work logically!

    
asked by anonymous 29.07.2016 / 21:59

2 answers

6

From PHP 5.6 there is a new feature to create an alias

link

Example:

use function minhaapi as ma;

For previous versions, there is not much to do. Usually we use lambda functions or gambiarras with variable variables.

    
29.07.2016 / 22:08
1

I researched a lot and found no alternative. The way you are doing is elegant and also use it a lot. I usually use it to make names of functions intuitive, so the programmer does not have doubts in the order of the function name, for example:

<?php

function string_convert($var) {
// do something
}

function convert_string($var) {
    return string_convert($var);
}

<?

If you have a PHP file with all the functions (many worthwhile) already standardized with functions named as MinhaApi_<algumacoisa> , you can create a PHP script that will open such a file, create and write to a new PHP file as "sibling" functions that already exist, assigning the new name and returning the original function. Otherwise, I recommend doing it manually, as you did in the first example.

    
29.07.2016 / 22:18