Namespaces are used to group, encapsulate functionalities. It is basically used to avoid name conflict. It works as a surname for your members, so you can have two João
s in its application provided that a Silva
and the other Oliveira
.
In this case the use
serves only to indicate that this family is available for use, ie all its members are accessible. But the load of the members will occur only on demand, after all the family is available to be called, but if you do not need a member, there is no reason why it should be present physically. It serves more as an indicative for the interpreter to know that the members of a namespace can be used.
Already the trait exists to enable code reuse in classes that only allow simple inheritance. It is a way to add predefined behavior to a class.
In this case the use
has completely different purpose, it indicates to the class that it must include among its members all members of the trait specified. The trait is an integral part of the class, without it the class would not be complete, the content of the trait is fundamental for the class to function, not optional. Even if an instance does not call any member of trait he needs to be in class to complete it, you can’t have "lame classes".
These two features share essentially the same syntax for the inclusion of external code but have completely different semantics and are conceptually distant. There is no relationship between its use other than the fact that obviously, by chance, a trait may be a member of a namespace.
I don’t know much about the subject in PHP either but if it’s the same as other languages, the two concepts are so different that I don’t know if you can compare.
– Maniero
It is not comparing the functioning, only the reason to declare the use in the trait loads the file at the time, while with namespace only when using the class.
– Papa Charlie
I get it, I’ll try to answer.
– Maniero
That’s basically what @bigown said in his reply. A
use
has nothing to do with another. Another example is the use of afunction
, that in the declaration of methods has a role and within an anonymous function has different behavior– gmsantos
True, the use used in both cases made me expect equal behavior.
– Papa Charlie