Advanced PHP Programming – How To Use Namespace In PHP

For PHP beginners, the concept namespaces seem quite complex. But, once we understand the basics of namespaces, it is easy to implement. In this article, we study how to use namespace in PHP.

As a developer, you will get a feel of advanced PHP programming while using the namespaces in your code.

PHP introduced namespaces to avoid naming conflicts between classes, functions, and constants.

PHP Namespaces

When we work on a large project, there might be possibilities of programmers giving the same names to classes, functions, or constants. For instance, if 2 programmers define the same class A then PHP throws a fatal error like cannot redeclare class A.

Define Namespace In PHP

To understand when we should define namespace we will write some piece of code.

Let’s say one developer has written the below code in a john.php file. This code is just an example of explaining namespaces. For simplicity, we write functions, classes, and constants in the same file.

function my_name() {
    echo "John";
}
 
class Programming {
    function my_language() {
        echo "PHP";
    }
}
 
const MYCONST = 1;

We got another developer who also used the same name for classes, functions, and constants but for different purposes(outputs). His file name is sam.php.

function my_name() {
    echo "Sam";
}
 
class Programming {
    function my_language() {
        echo "Python";
    }
}
 
const MYCONST = 2;

Now when both developer’s files are included in our project we will get the fatal error. The reason for the error is, we cannot use the same name twice for a class or method.

This is where we should define the namespace.

In both PHP files, we can define namespaces like below.

<?php
namespace John;
 
function my_name() {
    echo "John";
}
 
class Programming {
    function my_language() {
        echo "PHP";
    }
}
 
const MYCONST = 1;
?>

And

<?php
namespace Sam;
 
function my_name() {
    echo "Sam";
}
 
class Programming {
    function my_language() {
        echo "Python";
    }
}
 
const MYCONST = 2;
?>

Once, we added namespaces to the above files, PHP errors will be gone.

Using Namespaces

At this stage, we are done with defining namespaces. The next step is how to use namespaces in PHP.

To use the method, class, and constants of both programmers we need to write a code as follows.

<?php
require_once('john.php');
require_once('sam.php');
 
Sam\my_name();  //output "Sam"
John\my_name(); //output "John"
 
$a = new Sam\Programming();
$a->my_language();   //output "Python"
 
$b = new John\Programming();
$b->my_language();   //output "John"
 
 
echo John\MYCONST;  //output "1"
echo Sam\MYCONST;   //output "2"
?>

For accessing classes or methods we have to append the namespace name before it.

We hope you understand how to use namespace in PHP. If you have any questions or suggestions please leave a comment below.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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