Namespaces and Use when to use and for what purpose?

Asked

Viewed 11,677 times

5

What are namespaces and use and when it is recommended to use them in an application ?

  • 1

    I think this answers the question http://answall.com/a/16690/3635 even if it is about another language, now you want to know how to implement namespaces with "packages" I recommend reading this: http://answall.com/a/88039/3635 and http://pt.stackoverflowcom/a/91512/3635

2 answers

20


The namespaces are similar in several programming languages, as this answer explains How namespaces work in C#? (even though the subject is a different language)

I make up the words of Maniero my:

It doesn’t encapsulate anything

This understanding shows that despite the view that programmers have about the namespace being a module, a type box (as the first function indicates), it actually works as a surname for the types. A surname works as a way of naming a family. So you can have two Ricardos in the same environment without confusion, because one is Oliveira and the other is Silva.

Having understood what the utility is, let’s understand how it works in PHP. In languages like Java, Actionscript and C# namespaces are associated with files "natively", whereas in PHP this does not occur, you have to use require or include manually, meaning I have to create something like this:

foo.php

<?php

namespace Foo\Bar;

class Baz
{
     public function hello()
     {
          echo 'Olá mundo!';
     }
}

index php.

<?php

use Foo\Bar\Baz;

require 'foo.php';

$test = new Baz;

The use of "use"

The use does nothing, even with spl, in PHP it only serves to create nicknames, for example suppose you have two classes that the name is the same, if you do this:

<?php

use Aa\Bb\MinhaClasse;
use Xx\Yy\MinhaClasse;

At the time of using MinhaClasse it would conflict, so you can use so:

<?php

use Aa\Bb\MinhaClasse;
use Xx\Yy\MinhaClasse as MinhaClass2;

$a = new MinhaClasse;
$b = new MinhaClasse2;

That’s the moment you wear this:

<?php

use Foo\Bar\Baz;

$test = new Baz;

You’re just creating a "quick nickname".

There is also the possibility to use directly:

<?php

$a = new \Aa\Bb\MinhaClasse;
$b = new \Xx\Yy\MinhaClasse;

Note that within namespaces you can add other, like functions and believe even perform some things, example functions:

<?php

namespace Foo\Bar;

function file($path) { ... }

This way will not conflict with the native php function called file:

file('oi.txt');
\Foo\Bar\file('oi.txt');

Read about native functions and namespaces here:

Bar " " is required at the beginning of native functions when we use namespace?

THE SPL

SPL is a set of native PHP functions and classes that have been created to solve a number of situations, such as PHP does not load natively, but it is possible to use spl_autload, as I explained here What is spl_autoloader_register in PHP?, in this way files will be associated by folder splitting to namespaces, similarly to C#, Java and Actionscript3 languages.

Extras

As @Wallacemaxters explained in /a/104479/3635, this has been implemented since PHP5.3, well before PHP7

@rray also gave a hint of how to "write less" (only for PHP7), so you can group the classes of a namespace /a/104426/3635, example:

use Foo\Bar\DB\{Relaciona, NoSQL, LDAP};
  • 1

    Thank you very much, reading some answers here and on the net, I got the concept well.

1

In accordance with the official PHP documentation about namespaces, is basically a way of encapsulating items.

In practice, it serves to better organize your code, grouping classes and avoiding the conflict of names, such as with third-party classes.

You can use it to separate modules, groups of classes or the way you understand it will better organize and encapsulate your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.