Language:

Search

What are namespaces in PHP?

  • Share this:
What are namespaces in PHP?

Introduction

At first namespaces might look a bit unnecessary or might also look a bit complicated. But as a matter of fact, the concept of namespacing is very straight forward. Namespaces actually help organises your code & prevent naming conflicts inside your project's code-base.

In this short article, we'll be covering on how and when to use namespaces in your project, implementing psr-4 in our project & finally autoload them in.

Also Read: PSR 4 Autoloading PHP files with Composer

Basic project structure

Let's create a new project's directory & name it as understanding-namespaces. Create a new file named index.php inside our project's directory. This is the file where we normally require all of the working files one by one if working on a simple PHP project. This technique is commonly used in WordPress as well. but we'll be building a more structured approach for our project.

A simple Class

Let's create a new class FooBar.php and put it under /app/Classes/ directory.

class FooBar {

   public $foo='bar';
}

We've created a public property to test out out class. Now back in our index.php, we want to instantiate this class and call on our newly created property.

$foobar = new FooBar();
echo $foobar->foo;

Well, this will rather display an error, traditionally you'll have to require the class in your file once & that includes requiring each and every file in one single file, resulting in a bunch of require statements. quite a mess?eh?

require_once 'app/Classes/FooBar';

$foobar = new FooBar();
echo $foobar->foo;

Solution

To fix this problem, we can write some identifier for our class, that may differ it from other classes and can be useful while pulling into our main file. These identifiers are called Namespaces.
So the namespacing we'll be creating here is directly related to psr-4 autoloading, which we'll get to in a while.

namespace WebTier\Classes;
class FooBar {

   public $foo='bar';
}

We just used the namespace keyword to define our namespace. WebTier is like our project or vendor name, it could be anything at all. we're referring our app directory to WebTier here. This is where psr-4 autoloading will play its role.
This is very useful in minimizing the name conflicts between the classes in your projects.

Still not working out?
Now back in our index.php, if we require and trying to echo out the output, it displays an error of Class not found. What did we miss? Well ,remember our namespace is WebTier\Classes not app\Classes. To fix this issue,  we'll use the use keyword to import the class such as:

use WebTier\Classes\FooBar;
require_once 'app/Classes/FooBar';

$foobar = new FooBar();
echo $foobar->foo;

This might solve the problem for now, but still we're lacking the autoloading feature to get rid of this requiring files everywhere once and for all.

The PSR-4

Hopefully the basic concept of namespacing is a bit clear. For autoloading we'll be using composer to autoload our files in. This means is we'll be hearing to psr-4 for namespaces and then creating a directory structure which allows us to automatically load files in based on their namespace.
To get started, lets create a composer.json file inside of our project's root.

{
    "autoload" : {
        "psr-4" : {
            "WebTier\\" : "app"
        }
    }
}

"autoload" is basically a property and psr-4 is how we want to autoload. Inside of psr-4, we have two things:

- The Top Level namespace: which in our case, we named it WebTier

- The referring directory : which is app

Also Read: Convert Array to Object in PHP

Make it work

Open up your terminal and enter the following command to automatically autoload all of our files inside app directory.

composer dump-autoload -o

This will create a new vendor directory inside your project. For now it only contains a file named autoload.php which we'll be pulling in inside of our index.php

require_once 'vendor/autoload.php';

Final words

Play around with in your project, create new directories and Classes and give them a proper namespaces and see if that works for you. If don't leave us a comment below on where you got stuck. We'll be here to help. You can also follow us on Twitter

 

 

Usama Muneer

Usama Muneer

A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.