How does namespaces work in PHP?

Asked

Viewed 1,684 times

25

The namespaces were implemented from PHP version 5.3.0 as a form of "encapsulating items".

I never really understood his real concept, and I always get confused with autoload of classes.

Could someone explain to me, in a simple way, how it works namespaces, why they are used and if they possess something with autoload of classes?

Example:

namespace App\Http\Controllers; // usando namespace
use App\User;  // usando autoload
  • 1

    One of the answers in my question talks about him and has some examples including: http://answall.com/a/101738/20555, but I haven’t seen too much yet and your question will help me too. + 1

  • Organize code, avoid function name conflict... avoid risk of collision. It is used to avoid conflicting definitions and introduce greater flexibility and organization into your code base. To use, the programmer needs to be very organized, because it is useless to use the resource without having solid knowledge in OOP.

2 answers

14


Namespaces are mainly useful to avoid name collision.

Implemented in PHP 5.3.

It is very common to see older PHP libraries using a standard that consists of using underline in class names, to avoid conflicts.

This was necessary because, as many libraries were emerging, the names began to become "limited" because there could be collisions of names in common.

A great example is to create a class called Client. At least almost every library I’ve installed in a project has that name. That’s where namespaces come in.

We can see an example of how this problem was solved in the old days.

Example in older versions:

 Zend_Server_Client

New versions:

 Zend\Server\Client

In versions of PHP 5.6 they become even more useful, since with the new functionality of use function, it has become easier to also have a repository of functions.

Example:

 class WallaceMaxters\Helpers;

 function print_r($valor)
 {
     echo '<pre>';
     \print_r($valor);
     echo '</pre>';
 }

This function I created called print_r will not have collision with print_r on account of the namespace. But to use it in versions prior to php 5.6, you’d have to do something like:

WallaceMaxters\Helpers\print_r($_POST);

Or else:

 use WallaceMaxters\Helpers as h;

h\print_r($_POST);

But in PHP 5.6, like the classes, you can create a alias location for that function.

use function WallaceMaxters\Helpers\print_r as pr

pr($_POST);

Autoload

Class autoload is a feature added for classes to load (include and require) automatically as soon as instantiated.

So instead of you having to include every time a class that will instantiate, you simply set a global rule for loading the class.

An example of this is the PSR4, whom I deeply love.

Standard that is widely used in libraries that can be installed by Composer;

Patterns

The standard that most repositories use for their libraries for using the namespace is :

NomeDoFornecedor\NomeDaBiblioteca\NomeDaClasse

Example:

namesace Laravel\Database;

class Eloquent {}
  • Help me out there, Brother. I gotta get back from lunch kkkkkkkkk

  • Old libraries did not use PSR-0. It was born when namespaces already existed. Use _ were used well before the PSR-0 and it only supported these types of classes to maintain compatibility with very old codes.

  • Thanks for the info @gmsantos. I’m not from before php 5. This is what I put when I see old classes

  • 2

    Worth an edit on your reply, to prevent misinformation from being passed on.

  • Face I really traveled, sorry there. I confused the issue of psr-0

  • 1

    Value @gmsantos. You were gone, man

Show 1 more comment

10

Namespaces are used to group items (classes, constants, or functions in php) usually this is determined by the affinity between them. Just as a class groups properties and methods to solve a problem (single responsibility) with namescape happens the same but in a larger scope, in other words it defines what are the 'functionalities' of that module.

The php namespace is not as practical as the packges of java, an advantage would be to load for example the entire module of i18n that provides classes for date formatting, currency values etc or the persistence module that provides various classes for reading and writing information on different types of technology such as relational database, NOSQL, LDAP etc.

In PHP7 there is a slightly improved way to load a class group

use app\persistencia\{DBRelaciona, DBNoSQL, LDAP};

Manual - namespaces v7

Browser other questions tagged

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