Differences between traits and namespace for loading

Asked

Viewed 296 times

6

Using namespace the file is uploaded at the time when some method is used say, If you do not invoke anything from class the file will not be loaded.

use World;
class Hello {
    World::say()
}

Using trait even without invoking method or property the file is loaded at the time of use.

class Hello {
    use world;
}

I am "starting" on traits, my doubt is about this behavior so different from the namespace. Because the fact of declaring USE uploads the file while namespace nay?

This is a characteristic of traits or there is some configuration that can define the loading of traits only when something is invoked?

  • 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.

  • 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.

  • 1

    I get it, I’ll try to answer.

  • 2

    That’s basically what @bigown said in his reply. A use has nothing to do with another. Another example is the use of a function, that in the declaration of methods has a role and within an anonymous function has different behavior

  • True, the use used in both cases made me expect equal behavior.

1 answer

6


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ãos 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 understand, I think what led me to doubt was the use of the same reserved word use. Thank you

  • 2

    Some user found that I wrote nonsense and denied the answer, too bad it will not help people on what is wrong.

  • 1

    They thought I wrote nonsense too, but it’s not about the right or wrong content.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.