Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP namespacing benefits

Tags:

namespaces

php

After doing some research about namespacing in PHP I'm trying to figure out what are the real benefits other than having library items grouped together and re-use of the same class name.

What I don't like about namespaces are these extra lines at the top of the file you're calling the classes from 'use core\whatever\class', which you don't have to do with the standard approach.

Also - something I couldn't find information about - what happens in the situation where we need to use two classes with the same name, but in the different folders, which contain the same method name? We would still have to call the namespace, but how would the code figure out which method from which class should be used?

I've just started with namespaces so excuse me if this question seem to sound very basic.

Also - how does it work with the static methods - things like Helper class for instance - do I also have to indicate that I need this namespace 'use core\whatever\Helper' ?

like image 456
user398341 Avatar asked Feb 24 '12 08:02

user398341


People also ask

What is the main purpose of Namespacing in PHP?

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.

What is the advantage of namespace?

The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.

Can I use two namespace in PHP?

Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.


2 Answers

If you have a large project with many classes, you'll eventually run into naming conflicts. You have a db/mysql/adapter.php and a http/curl/adapter.php. To deal with this without namespaces, you have to give your classes unique names like Db_Mysql_Adapter and Http_Curl_Adapter, and any time you refer to these classes you need to use their full name.

Namespaces allow you to name your classes Db\Mysql\Adapter and Http\Curl\Adapter and refer to them simply by Adapter in their local namespace, or Mysql\Adapter and Curl\Adapter respectively in other namespaces. This can save a lot of typing.

Just browse through the source of Zend Framework version 1 vs. version 2 to see the difference.

what happens in the situation where we need to use two classes with the same name, but in the different folders, which contain the same method name? We would still have to call the namespace, but how would the code figure out which method from which class should be used?

If you're in namespace Db\Mysql, Adapter::foo() refers to Db\Mysql\Adapter and \Http\Curl\Adapter::foo() would refer to the other one. You can also alias the class at the top of your file, if you don't want to write out the whole name all the time:

namespace Db\Mysql;
use Http\Curl\Adapter as HAdapter;

Adapter::foo();  // Mysql adapter
HAdapter::foo(); // Http adapter, same as:
\Http\Curl\Adapter::foo();
like image 152
deceze Avatar answered Oct 23 '22 14:10

deceze


What I don't like about namespaces are these extra lines at the top of the file you're calling the classes from 'use core\whatever\class', which you don't have to do with the standard approach.

Namespaces group classes logically and avoid polluting the global scope. It also prevents you from making a big chunk of PHP code - code which may have parts needed in other places in your site. This makes it more reason to place it in another file for another part of the site to use. Don't Repeat Yourself (DRY), use in in many places but only code it once!

what happens in the situation where we need to use two classes with the same name, but in the different folders, which contain the same method name? We would still have to call the namespace, but how would the code figure out which method from which class should be used?

Namespacing prevents these issues, using aliases. besides, having classes with the same name? unless they are the same name but different purpose or different subcategory, like api.document.events and api.ajax.events. Otherwise, it's bad program design.

like image 1
Joseph Avatar answered Oct 23 '22 14:10

Joseph