About PHP Namespace, Sub-Namespace and Class sharing the same name?

Well the title may look a bit confusing, but I have a real scenario that may explain a bit more. In my application there is a folder called Model, which represents the namespace Model. There are various sub-namespaces inside the Model namespace, one of them is called DomainModel. I also have a class called DomainModel, which is the root class for all domain object classes. Now heres the question, what if I place the class DomainModel inside the Model namespace? The simple code below will demonstrate the idea:

use Framework\\Model\\DomainModel;

In this case, I was wondering how is PHP able to tell whether the use statement above is referred to as the DomainModel sub-namespace, or the DomainModel class? In a file system you have file extensions that distinguish a directory and a file, but in namespace its much harder to tell. Will it throw an error message with name conflict? Or will it actually load both the DomainModel Class and DomainModel namespace in an intelligent way? Just curious.

At the use statement, PHP doesn’t know. Only at the point of invocation does PHP decide based on context.

new DomainModel(); // resolves to new Framework\\Model\\DomainModel()

new DomainModel\\SomeDomainModelClass(); // resolves to new Framework\\Model\\DomainModel\\SomeDomainModelClass()

I see, interesting. So PHP wont throw a fatal error in this case? Thought it is not a good practice though.

Which of course is exactly why you would not do this in the first place regardless of how PHP processes namespaces. No point in intentionally making your code harder to understand.

Need a base domain model class? Call it AbstractDomainModel and stick it in the DomainModel directory

If you really really want to have a .Framework\Model\DomainModel class then make a Framework\Model\DomainModels directory for any derived classes.

I see, this makes sense. I wonder what if you have a class inside a subnamespace that shares the same name. So instead of Framework\Model class, I now have a class called Framework\Model\Model. Is this gonna be a problem for PHP to handle namespace resolution too?

Framework\Model\Model will work as expected under PHP.

But from a developers point of view, if Model is a base or abstract class (and thus not really a model at all) then why not call it AbstractModel? Your other files would have names like Product, order etc.

Consider executing the following line:

php composer.phar create-project symfony/framework-standard-edition path/ 2.3.3

Then browsing through the vendor directory. Lots and lots of namespace examples.