📜 ⬆️ ⬇️

The benefits of namespaces

In the latest versions of PHP appeared namespace 's - namespace.

They have obvious advantages:
- help to reduce the names of classes;
- allow you to avoid name conflicts;
- help to better structure the classes.

But, in addition to this, there is another advantage that is not so 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 class declaration:
require_once "MyClassParent.php";
require_once "MyClassDependency.php";


Then PHP5 came, __autoload () and spl_autoload_register () appeared - we removed the unconditional file connections and the overhead with them. Everything is good, but only now it has become not so obvious, what other classes / files are needed for our class in order for it to function normally.

And then a php-5.3 trampoline appears. Now, without an extra overhead projector, we can, and even not only can, but in certain cases should - indicate the dependencies of our class:
use myns::MyClassParent;
use anotherns::MyClassDependency;


Note that using use does not connect files with classes.
Now, as the saying goes, programmers and wolves are fed and the sheep are intact.

Source: https://habr.com/ru/post/38097/


All Articles