Difference between revisions of "Import and Nickname Namespaces in PHP"

From WikiHTP
(Created page with "Alternatives to import namespaces or members of namespaces, with or without aliases. Rules for importing and how to access the global scope to resolve names used in namespaces...")
(No difference)

Revision as of 23:52, 18 November 2019

Alternatives to import namespaces or members of namespaces, with or without aliases. Rules for importing and how to access the global scope to resolve names used in namespaces.

Import with aliases

When importing code from namespaces it is possible to assign them an alias to refer to them later. This can be useful for several situations, such as giving you a summary way to access a class within the archive or to assign it a different name, in case the original name of that class is already occupied so that we could avoid collisions of names on certain occasions.

use Deswebcom\Galaxies\Andromeda\Star as E;

So we would be assigning the Alias ​​"E" to the "Star" class, which is in the namespace "Deswebcom \ Galaxies \ Andromeda".

From there we could use the fully qualified class name, "Deswebcom \ Galaxies \ Andromeda \ Star" or the alias we just defined "E".

E:: belongs();

O well:


Note: Remember that you can also declare that you will use functions or constants from other namespaces. In that case, the thing changes something, because you have to mention "use function" or "use const".

For example, I have this namespace.

namespace Midesweb;

function quien(){
    echo "I am @midesweb";

And in another file, I want to use the function who, also for an alias.

use function Midesweb\quien as q;

Now that function is available with the alias "q". Then I can invoke it like this:


Actually this theme of the aliases was already using it, although we had not mentioned it yet. This is because any statement "uses" what it really does is create an alias. Only, if we do not indicate a different alias, create that resource with its original name. For example:

use Deswebcom\Galaxies\Andromeda\Star;

It is a line of code equivalent to:

use Deswebcom\Galaxies\Andromeda\Star as Estrella;

Before finishing this point I would like to give two clarifications, the same are obvious things, but I think I can solve the doubts of some readers:

  1. A sentence used with an alias (or without it, because we really already know that the alias always creates it when declaring a "use") makes available code only for the .php file where we created that alias.
  2. This stems from the above: If you are within a namespace and declare a "use" to create an alias of a class, function or anything else, it does not mean that you are adding that element to the namespace in which you are. That function or class, interface or whatever, remains in the namespace where it was defined and with that "use" you are simply indicating that within this file you are able to refer to that element by its alias.

Import a full namespace with aliases

Another common task is to create an alias to refer to a complete namespace.

use Deswebcom\Galaxies;

That creates an alias called "Galaxies" for the namespace "Deswebcom \ Galaxies".

If within the namespace Deswebcom \ Galaxies there was a function called distance (), then, thanks to the alias, we could invoke it in this way:

Galaxies\distance("Milky Way", "Alpha Centaur");

In addition, thanks to the Alias ​​we can also access namespaces that are within Galaxies in a summarized way.


Here we have moved from the namespace "Galaxias" to the subnamespace "Andromeda" and from it, invoked its function locate().

We could create that namespace with a different alias if we wish.

use Deswebcom\Galaxies as GA;

Then we will use that abbreviation (alias) to access the namespace members.

GA\distancia("Milky Way", "Alpha Centaur");

Import only in the global scope

The instruction used to import a namespace or a member can only be done from the global scope of a file. That is, if we try to import from a function it will give us an "unexpected 'use' (T_USE)" error.

function importar_ilegal(){
    use Deswebcom;

    echo "You can't do this, because this is not the global scope";

Nor could you do it from a class:

class WebSite{
    use Deswebcom;

But well, in this case, the error would be different, because here PHP would be assuming that you are going to use a trait in the "WebSite" class and not the namespace "Deswebcom".

Global namespace

If we are programming within a namespace we can create any member with any name without colliding with other members of other namespaces. This happens with the global scope too.

All PHP native functions are defined within the global scope, so sometimes we may need to refer to the global scope instead of the namespace scope.

For example, I have the namespace "Deswebcom \ Galaxy" and within it, I have defined a function called explode (). In that case, within the namespace "Galaxy", explode () will refer to the explode() method that we have defined in this area.

namespace Deswebcom\Galaxies;

function explode($star){
    echo "The star $ star will explode";


That code will return you "The Sun Star is going to explode" because the newly defined explode() is being invoked.

Note: You can repair that if we were not within a PHP namespace we would not have allowed us to create a function called explode() because there is already a native PHP API function with that name, which is responsible for separating parts of the string in an array.

Now, if you need for some reason to access the explode() function of PHP, the native one defined in the global scope, then you would have to specify that this function is that of the global scope and not the one defined in the namespace. To do this, you just have to add a counter bar before the name of the function (or anything else in the global scope that we want to access).

$arraymarks = \explode("|", "Webdevelopment| school");


With all this, you have enough information about namespaces. You really know everything you need, so from now on try to use them.