Namesake: An Introduction to Namespace in PHP Programming

Posted by : Diana Middlebrook

Namespace: An Introduction Let’s say, you have a cupboard of your own, and you put everything into it. You put your personal belongings, your pencil, ruler or even your graduation certificate into it. The cupboard next to it belongs to your nagging roommate, who sometimes messes around with your stuff, mixing them up. He has the identical things in his cupboard. So you take the initiative to rename the cupboards so that the items do not mix with each other. This is somewhat namespace does in PHP program development.   Even before PHP 5.3, PHP developers to identify the classes, functions or even constants and to separate the code bases specifically used underscores. This instance can easily be compared to your cupboard, which you labeled to separate the things from your roomy. To put it in a nutshell, you would be able to define the same function, classes, interfaces and definitions in separate namespace.  Namespace can be defined as hierarchically labeled code block, which has the PHP code, which helps in avoiding naming collision and help in aliasing original names. Two of these features help in improving the readability of the source code.   When you are not even using namesake, the element is being used without your knowledge. The namespace, which are not user defined, it is called the global namespace.   To talk about the global namespace it also holds the PHP definition such as the exception class. The global namespace thus, does not have a unique identifying name of its own. So, it gets the nomenclature of Global Space.   Debunking the Namespace Definition When it comes to namespace definition, it is the first statement, which a PHP interpreter would come into contact in a particular PHP file. Only one statement, which is allowed to occur in the declaration, it is the namesake declaration.  This can occur only with the declaration of the encoding the script.   For the PHP programmer declaration of the namesake is a cakewalk. The only thing the developer has to keep in mind that namespace should go by the rules of identifiers in the PHP. It becomes mandatory for the namespace to begin with a letter or an underscore, which should be further followed by letters, numbers and underscores. One of the biggest benefits of namespace is that you are being able to use multiple name spaces for a single file.   The subject of namespaces can be used in various ways. The following lines of the article would take into account the different types of namespace, which are being used in the industry.   High-Level Namespace The high-level name spaces can be taken into considerations when you are building an application for the organization for a high-level domain. One of the biggest advantages of using high-level domain is that the organisation by itself can give PHP code context while avoiding any kind of naming collision. But there might be slight chances of naming collision if you involve too much sub division for different purpose within the application.   Sub-name spaces The sub name spaces are the ones which are embedded within the high name spaces. These namespace are created to clarify the PHP class you would be creating. But there is a word of caution for you before using sub-name spaces.   Do not create too many of these. They become too difficult to organise and might get out of your hand if they are many.   For the seasoned PHP programmers, they restrict themselves to at the most seven namespace. Some of the common places you would be getting hold of sub-namespace are mainly from project or application names and the other by domain names.   Definition of Namespace by Projects Even after providing the High-level namespace and still wants to projects to be defined by sub-name spaces   Definition of Name-Spaces by domains One of the very common ways of choosing the sub-name spaces is by the name of domains. To define domain it is a classification or grouping, which is found in the larger domain problem. When it comes to domains you can always take the example of the ‘Customers’ or ‘Products’ in a particular bigger application you are dealing with.   Reusability and Name Spaces  The namespace, when named by you, helps in giving them an implied purpose along with its reusability. Don’t ever go by poor naming choices as it hinders reusability of classes. Poor names show that the class is limited to a specific purpose. Specifically named namespace can be used to store classes and can be reused across applications.   Aliases and Namespace In case of PHP programming, namespace might help you to avoid naming conventions; they have their own share of shortcomings. The namespace have difficulty with longer names. Thus, PHP namespace gives the PHP developer the opportunity to use shorter alias name for their code.   Whenever you are planning to use Namespace, it is best to use Uppercase Camel or PASCAL.   Conclusion This article is definitely an eye-opener for you about namespace in PHP application development. Take namespace as your friend, which can help you in organizing code, avoiding naming collisions as well as providing context for your classes. Take namespace as a tool, which can make your code properly referenced and used.

Author Info

author

Diana Middlebrook

Diana Middlebrook is an ardent software developer who works at GetAProgrammer, an esteemed Sydney-based software development company. Being passionate about developing custom solutions deploying state-of-the-art technologies, she likes to share her thoughts on the latest technologies and trends in the software industry

10
Years Experience
6
Countries
150 +
Geeks
600 +
Global Clients