Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are namespaces really all that useful in frameworks?

As far as I can tell, the only reason we have namespacing in PHP is to fix the problem of classes (+ functions & constants) clashing with others classes of the same name.

The problem is that most frameworks setup their autoload and filesystem hierarchy to reflect the names of the classes. And no-one actually require()s or include()s files anymore.

So how does namespacing help this any? Either the class is loaded based off of it's name:

new Zend_Db_Table_Rowset_Abstract;

or off it's namespace

new Zend\Db\Table\Rowset\Abstract;

Either way I am stuck with only being able to create one class with this name.

/var/www/public/Zend/Db/Table/Rowset/Abstract.php

UPDATE I'm not sure I'm getting the point across.

Even if I created two files with the same class Zend\Db\Table\Rowset\Abstract in them I still couldn't use them together since they both claim the same namespace. I would have to change their namespace names which is what we already do!

This leaves me to belive that the only use for namespaces is function names. Now we can finally have three functions all named the same thing!

Or wait, I forgot you can't do that either since each requires the namespace prefix!

a\myfunction();
b\myfunction();
c\myfunction();

Taking ircmaxell's example:

$model = new \Application\Model\User;
$controller = new \Application\Controller\User;

How is that any different than without?

$model = new Application_Model_User;
$controller = new Application_Controller_User;

This is also a neat sounding feature - but what does it truly do for us?

use \Application\Model\User as UserModel;
use \Application\Controller\User as UserController;

$foo = new UserModel;
$bar = new UserController;

Now you cannot have a class named 'UserModel' since you have a namespace setting for that term. You also still cannot have two classes named under the same alias.

I guess the good thing is that you can rename the long Zend_Db_Table_Rowset_Abstract

use Zend_Db_Table_Rowset_Abstract as RowAbstract;

leading to developer confusion about where the non-existent class "RowAbstract" is defined and coming from in the system.

like image 396
Xeoncross Avatar asked Jun 30 '10 21:06

Xeoncross


People also ask

Are namespaces useful?

So namespaces themselves are really good, they help make code more organised and they allow you to know where functions are coming from rather than you looking at what header files you have in your cpp file and making a guess at where that function came from.

Is namespace necessary in C#?

Yes. It'll help prevent clashes with identically named classes in other namespaces without having to resort to the (IMHO) ugly use of global:: .

What is the use of a namespace in web development?

Namespace is a context for identifiers, a logical grouping of names used in a program. Within the same context and same scope, an identifier must uniquely identify an entity. In an operating system a directory is a namespace.


1 Answers

My impression is that namespaces are used in a cargo cult programming fashion. Because it's new, it gets used like crazy. Deeply nested namespaces, like in Doctrine2, add no further protection against name conflicts. And it's very obvious that \nested\name\spaces are just used to achieve a 1:1 mapping to directory/file names. Clearly a code smell.

But I suppose this phenomenon is also caused by trying to mimick Java module names in PHP. And furthermore the backslash syntax doesn't convey a sensible semantic as in other languages.

Anyway, the ability to rename classes while importing namespaces is a big bon. That's useful when it actually comes to mixing conflicting definitions. And the namespace syntax can simply be added, once an actual name conflict arises. (I see no need to implement namespaces right away, when name conflicts are that rare and for most projects a purely fictional problem.)
If you do it only when required, the awkward namespace syntax never even has to rear its ugly head. If there is only one namespace level to import, you can just use namespace1\Class as LocalName and don't convolute the application with any namespace1\Class syntax. (Still it's a better idea not to use overly generic class names in namespaces.)

like image 128
mario Avatar answered Oct 12 '22 13:10

mario