About the benefits of namespace

    In recent versions of PHP, namespace's have appeared - namespaces.

    They have obvious advantages:
    - help shorten class names;
    - Avoid name conflicts;
    - help to better structure classes.

    But, besides this, there is another advantage that is not often paid attention to: namespace 's help the developer to see the dependencies of a particular class, and this does not lead to any overhead. I will explain.

    Remember how in PHP4 we wrote before declaring a class: Then came PHP5, __autoload () and spl_autoload_register () appeared
    require_once "MyClassParent.php";
    require_once "MyClassDependency.php";

    - we removed unconditional file connections and overhead with them. Everything is fine, but now it’s not so obvious what other classes / files are needed for our class to function normally.

    And here - the php-5.3 trampoline appears . Now, without an extra overhead, we can, and even not only can, but in certain cases should - indicate the dependencies of our class: Note that when using use , files with classes are not connected. Now, as they say, both programmers are full of wolves, and the hard sheep are safe.
    use myns::MyClassParent;
    use anotherns::MyClassDependency;

    Also popular now: