Good day, dear colleagues. Actually, at his leisure, he looked at the man and remembered the namespaces in php, attention to the question: What is the real practical meaning of using them?

I’ll enter not __autoload with __autoload , etc., there you can do without them, but what is the real use? Personally, I suspect that I didn’t have any (once I remembered them by chance and after learning the basics of php I never returned to them), although again we are all sinners and maybe I’m writing heresy ...
If I am mistaken (and I wrote the heresy), I would be very grateful for a piece of some illustrative code, you can just poke a script, libu, etc. It is important that in the example you cited their use was justified and useful. Please shed light on this question, thanks in advance :)

PS: Why the hell they ask about them at the interviews, I don’t understand at all ... They asked a friend, but he didn’t know what it was, although IMHO is a normal programmer ...

  • Along the way, this is something from the crosses)) By the way, I didn’t know either, thanks) - Sh4dow
  • I read this <a href=" habrahabr.ru/blogs/php/72033/"> this </a>. The quality and assertiveness of PR reminded bitcoin. Summary - bun / fancy dress / bow, practical meaning is only for those who "use MyLib; echo MyFunc();" writing is easier than "echo MyLib::MyFunc();" - Sh4dow
  • Well, it's not easier for me, I'm friends with OOP :) <br> thanks vseravno, I suspect that asking such a job interview for a middle programmer is nonsense ... In Karoche, another uselessness like <b> goto </ b> for a campaign .. - Zowie
  • besides this is a freak of php5.3. Apparently, the essence of the question was to check whether a person is following the language updates. At least, then he is somehow logical) Oh, we didn’t know that php has goto O_O The day of discovery is straightforward. This is, by the way, good * insidious look *, you can get around get_defined_functions ^___^ - Sh4dow
  • Well, for example, I taught biology at school (and perfectly well), but now I’ll not tell you anything but some general definitions ... And all because it wasn’t useful, it’s here if I didn’t come across mana today, not the fact that I would ever remember about it at all (and, I suspect, I would have lived and did not torment :)) <br> About get_defined_functions is harsh ^^ - Zowie

1 answer 1

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

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

A good example of using namespaces in PHP: Zend Framework 2

Although, probably, 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. Although in the latter the namespace essentially replaces the concept of modularity.

  • Why do I need namespace in C ++ and packages in java ... I didn’t scare me about frameworks. I often write without them :) <br> It was the real benefit that interested me, as long as I don’t see any sense from the given examples .. <br> As for the uselessness, I think so, but you never know :) - Zowie
  • > Why does the namespace in C ++ and packages in java I understand perfectly ... Then it is not clear what the question is. In PHP, these are absolutely the same ones. With only one exception: they are not used anywhere (neither in the core, nor in the pecl-extensions, nor in the frameworks), and therefore do not represent any significant value. Now, if PHP developers took to version 6, they would rewrite all core and pecl functions with namespaces taken into account ... - Ilya Pirogov
  • And what is not clear then? There is such a bun that I know, but I didn’t see any sense in it, I remembered about it and decided to make sure I am right. Note the question is not what it is, but what is the use of them ... <br> Although I I agree that they are most likely useless, but extensions and frameworks are not an irresistible argument ... <br> <br> <small> PS: Angry Bird Well, the topic was not about namespaces at all, but about namespaces in php, why was it renamed then? </ small> - Zowie
  • In general, we conclude that there is no sense :) - Zowie