Custom PHP developer | Digital Agency in UK | Business Designz

Business Designz

Importance of custom PHP Namespace

Why you might need namespaces? and How to use custom PHP namespaces in your day-to-day development?

More often than not, you will end up using third-party libraries from different vendors in your PHP applications. Now, there could be collisions between two or more libraries if a library tries to reuse a name that’s already being used by another library in your application. As you probably know, if you have a class, function, or constant declared somewhere in your code, you can’t have another one with the same name elsewhere. If there’s a name collision, it’ll eventually break your application.

Before the introduction of namespaces in PHP, developers solved this problem by introducing different techniques that would prevent name collisions. One popular solution was to use long class and function names. For example, to introduce the Database class in their library, they would prefix it with the vendor name, and thus it would become like Vendorname_Database.

If you’ve coded in WordPress or the Zend framework, we are sure you’ve noticed such practices. To address this problem, namespaces were introduced in PHP as of PHP 5.3. The best way to understand namespaces is by analogy to the directory structure concept in a filesystem. The directory which is used to group related files serves the purpose of a namespace. You can’t

have two files with the same name in the same directory, but you can have files with the same name in different directories. Namespaces mimic the same principle in PHP!

Basically, namespaces provide a way to group related items together, avoiding any potential name collisions. Without a doubt, this has turned out to be one of the most significant recent changes in PHP. In fact, the majority of the third-party library vendors and frameworks have already adapted namespaces in their codebase.

Business Designz


Defining Namespaces and Sub-Namespaces

The first thing to do is to define a namespace with the namespace keyword at the top of the PHP file. All the code underneath the namespace keyword becomes namespaced code. It’s important to note that this keyword must be placed at the top of the file, making sure that nothing precedes it. The only exception is the declare construct, which you can use before you define a namespace.

How to Import Namespaces

You can import namespaces with the use operator, thus avoiding the need to specify fully qualified names.

For example, you import the Tutsplus Code namespace. Now, you just need to add the Code prefix when you call namespaced items, and it’ll automatically be converted into fully qualified names.

Aliasing Namespaces

Aliasing is similar to importing, but it allows you to reference long namespaces by specifying a shorter name.

Create the TC alias for the TutsplusCode namespace, and thus you could use it instead of specifying the fully qualified name.

In fact, aliasing is really useful when it comes to creating aliases for classes and interfaces.

Namespaces are really useful when it comes to structuring your codebase, along with the other obvious benefits they bring.

 business designz