Scroll to top

Namespaces in PHP

In PHP version 5.3 a new feature known as namespacing was added to the language. Many modern languages already had this feature for some time, but PHP was little late to the scene. Let’s find out why PHP namespaces can benfit our application.

In PHP you can’t have two classes with the same name. They have to be unique, The issue with this rule is that if you are using third party library which has a class name User, you can’t create your own class also called User. This is a real shame because that’s pretty common class right?

PHP namespaces allow us to resolve this issue, in fact, we can have many Userclasses as we like. Not only that, but we can use namespaces to contain our similar code into neat little package or event to show ownership.

Let’s take a look at a normal class initialization which we do regularly in PHP.
Global Namespace

<?php
// app/models/Test.php
 
class Test
{
    //code will go here
}
?>

There is nothing special about it, if we want to use it then we can do this.

<?php
 
// app/routes.php
 
$test = new Test();
 
?>

Everyone knows this just a normal class without having a namespace.

Simple Namespacing

Let’s create another class alongside original, global Test

<?php
 
namespace India;
 
// app/models/Test.php
 
class Test
{
   // code will go here
}
 
?>

Here we have another Test class with a minor change. The line namespace India informs PHP that everything we do is relative to the India namespace. It also means that any classes we create inside the ‘India‘ namespace.

Now when we try to use ‘Test‘ class once again.

<?php
 
$test =  new Test();
 
?>

This will give access to the first class but to access class which is in namespace India.

<?php
 
$test =  new India\Test();
 
?>

We can instantiate a class within a namespace, by prefixing it with the name of the namespace and separating with the backward(\) slash.

You should know that namespace can have many levels of hierarchy as they needed.

Now we are able to instantiate the Test class within namespace India, What if we are inside India namespace and we want to instantiate Global namespace Test class. let’s see how we can do this below.

<?php
 
$test =  new \Test();
 
?>

Now imagine we have another namespace called Usa\User. Now we want to use this class from within India namespace.

<?php
 
namespace Usa
 
// app/routes.php
 
$user = new \Usa\User();
 
?>

It’s simple just like how we include files in a PHP class. We have to go back to global namespace and then go to respective namespace then instantiate the respected class.

There was a nice shortcut to do this in a simple way using use statement like below.

<?php
 
namespace India
 
use Usa\User;
 
// app/routes.php
 
$user = new User();
 
?>

Using this new trick we can directly instantiate the class from any other namespace without using any backward(\) slash in the current namespace.

Now suppose we had same class in the current namespace India and we want to instantiate that class with the User in Usa namespace. There we had a problem with that we can do like below.

<?php
 
namespace India
 
use \Usa\User as Usauser;
 
// app/routes.php
 
class User {
   
// code will go here
 
}
 
// India User class
 
 
$user = new User();
 
//Usa User class
 
$usauser = new Usauser();
 
?>

For Ownership packages, it’s better to use a structure based namespace with your own name in the application of internal architecture like below.

John\Blog\Content\Post
 
John\Blog\Content\Page
 
John\Blog\Tag

There are limitations to the namespace in PHP which is not a bug it’s more like a limitation of usability if I say honestly. Like if you see other languages like java they also implement namespace in the same way but if you import a package in a namespace using import keyword it will import all the classes in that package but in PHP we have to include each and every class separately to use in the namespace using use keyword. For this what we can do use something like below.

<?php
 
namespace Website;
 
use John\Blog as Cms;
 
// app/routes.php
 
$post = new Cms\Content\Post();
 
$page = new Cms\Content\Page();
 
$tag = new Cms\Content\Tag();
 
?>

Thanks

KodeInfo

Author avatar

admin

http://www.kodeinfo.com
Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *