Jump to content

php namespaces


skaterdav85

Recommended Posts

i recently discovered php's namespaces feature. After reading some of the documentation, namespaces just seem like a way to alias the folder path of where a particular class, function, or constant resides. Is that essentially all of what namespaces are?Are namespaces similar to java packages? i wasn't too familiar with java packages but they seem similar, since they both allow you to group together related classes.

Link to comment
Share on other sites

Namespaces are simply prefixes you can attribute to symbols (of any type, not just class definitions), for disambiguation and collision prevention purposes. They don't need to correspond to any reality in the filesystem structure. I could equivalently create my own PHP "namespace" by manually inserting some prefix before my symbol names ($prefix_current, function prefix_do(), class prefix_Operate{}), but the formal namespace mechanism provides a more convenient and flexible way of doing this.

Link to comment
Share on other sites

oh ok. So say i have a script and in that script I am including 2 files. Each file contains a class named User. Obviously there would be a clash because you cannot have 2 class definitions with the same name (User) included in your script. If I namespace the first class to type1Users and namespace the 2nd to type2Users, would there not be this clash?

Link to comment
Share on other sites

Yup.Also, I was wrong before: only "classes, functions and constants" are covered by namespaces.

Link to comment
Share on other sites

oh ok. So say i have a script and in that script I am including 2 files. Each file contains a class named User. Obviously there would be a clash because you cannot have 2 class definitions with the same name (User) included in your script. If I namespace the first class to type1Users and namespace the 2nd to type2Users, would there not be this clash?
Would it not be better to reconstruct the two classes in such a way that they conform to the same abstract class? This would provide you with structural uniformity across the classes, ease of use, and the opportunity to create more similarly defined classes in the future.Roddy
Link to comment
Share on other sites

Would it not be better to reconstruct the two classes in such a way that they conform to the same abstract class? This would provide you with structural uniformity across the classes, ease of use, and the opportunity to create more similarly defined classes in the future.Roddy
probably in this case i would imagine you would use an abstract class. I was just trying to understand how namespaces work though. Im not too sure how often one creates classes with the same class name, but if you do namespaces are the solution so you dont have to think of another unique name for the 2nd class.
Link to comment
Share on other sites

There are cases where the classes have nothing to do with each other, besides the name - maybe two developers were working on one each and just didn't realise. Using namespaces, each developer could completely isolate their code and prevent any problems.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...