Practical implications of using namespaces in PHP


Good day, dear colleagues. Actually flipping through the man at my leisure and remembered about namespaces in php, attention to the question: What is the real practical meaning of their use?

I have to __autoload not __autoload with __autoload , etc., there you can do without them, but what is the real use? Personally, I suspect that nothing (since I remembered about them by accident and after learning the basics of php never returned to them), although again we are all sinners and maybe I am writing heresy …
If I am wrong (and still wrote heresy), I would be very grateful for a piece of some illustrative code, you can just poke into some open source script, lib etc. It is important that in your example, their use is justified and useful. Please shed some light on this question, thank you in advance 🙂

PS: Why do people ask about them at interviews, I don’t understand at all … A friend was asked, but he didn’t know what it was, although IMHO a normal programmer …


In general, the benefits of namespaces are tremendous. allows you to limit the scope of classes / functions / etc, eliminate name conflicts and does not care about their uniqueness (example of a class from the zend framework: Zend_Controller_Action_Helper_AutoComplete_Abstract , the absence of namespaces leads to such class names).

In PHP realities, namespaces are not only practically useless, but also incompatible with most modern frameworks. In order for them to be effective, you need to rewrite all the built-in php functions and, accordingly, the entire existing PHP codebase.

Good example of using namespaces in PHP: Zend Framework 2

Although, perhaps the best way to understand what namespaces are for is to learn some other language that was originally designed using namespaces. For example C #, Java or Python. While in the latter namespace, it essentially replaces the concept of modularity.

Scroll to Top