Hello!

I'm wondering what is the advantage of using namespace in php ?

Now I have all the classes connected through the autoload and the classes are located in accordance with PSR-0, that is, the class ClassParent_Class_ClassChild is located in /classes/ClassParent/Class/ClassChild.php

If you use the namespace, then only the way I will call the class will change for me: as it is now - ClassParent_Class_ClassChild , or with the namespace \ ClassParent \ Class \ ClassChild (I know about aliases, but I don’t consider it an advantage)

It is possible that I do not understand something. Therefore, I asked a question.

    1 answer 1

    Very good article on this!

    And specifically on your question, I reply with a comment from the article : using the namespace, we guarantee the existence of our libraries (my code and third-party) in completely different spaces, which means that in general no one will overwrite anyone.

    • Thank you, but I read this article. I know that the namespace gives additional nesting, but in my example this same nesting is provided by using the file structure of the application. And I specifically wrote that all classes are connected automatically (_autoLoad ()). That it works with the namespace? I also need to decompose class files into folders based on namespaces. Ie \ Core \ ClassName will be located in /Core/ClassName.php. - dmitrymomot
    • The “overwrite” is not excluded either with the option through the underscore or using the namespace, since for this it will be enough to put a class with the same name higher in the file system into the same folder (cascade file structure). - dmitrymomot
    • @DimkOf well the question has sounded, the public has answered! - Artyomich