Is it possible to have two classes with the same name if they're in different folders?

Chris picture Chris · Apr 13, 2011 · Viewed 18.4k times · Source

I was wondering if there is anything wrong with having two classes with the same name in PHP if they're in different sub folders, other than the obvious "human factor" of editing the wrong file by mistake?

I have looked for other posts relating to this, here and elsewhere on the web, but I didn't find any that could answer this specific question. I did however find this Autoload classes from different folders very helpful though, and in fact it solved one of my other questions.

Answer

Shakti Singh picture Shakti Singh · Apr 13, 2011

This is possible to have classes with same name even in same folder.

But Make sure you have loaded only one class in the PHP script at a time.

They can not be loaded in the same script at same time.

PHP does not know if you have created two classes with same name but the fact is PHP will not load them in same script. You can use one class at a time.

You can also look at namespaces in php.