KodeInfo | Learning resources for web and mobile development

Namespaces in PHP

December 3rd, 2014 07:16:31 by Imran Iqbal Comments(0) - Views(6654)

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 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, the 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 User classes 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 to it, if we want use it then we can do this.

<?php

// app/routes.php

$test = new Test();

?>

Everyone knows this this just a normal class without having 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 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 namespace, by prefixing it with the name of the namespace, and seperating 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 namesapce 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 with in 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 respect 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 instantiate that class with the User in Usa namespace. There we had a problem for 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 structure based namespace with your own name in the application 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 limitation of usability if i say honestly. Like if you see other languages like java they also implement namespace in th same way but if you import a package in a namespae using import keyword it will import all the classes in that package but in PHP we have to include each and every class seperately 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

  • Imran Iqbal
    Imran Iqbal

    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.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus