Reading list Switch to dark mode

    Namespaces in PHP

    Updated 17 June 2017

    Today we are going to learn about Namespace in PHP.  Let’s start with definition of namespace So “Namespace are basically a way of organising your PHP classes and preventing from any kind of code conflicts . See this example ,

    <?php 
    namespace Webkul;
    
    //We have here a namespace with name Webkul and a class Name Opencart
    
    class Opencart {
    
    //Your all code will be here for this class
    
    }

    In this example we have  created a  class called Opencart inside of the Webkul namespace. If we did not create namespace for class and  we have another class with the  same name of ‘Opencart’ included in this file  we would get an error stating we cannot re-declare class. Namespace will allow us to create two class name with same name and we can include both in same file.So we can create another class with same name  like this-

     

    <?php
    
    namespace Demo;
    //We have here a namespace with name Demo and a class Name Opencart 
    class Opencart {
    //Your all code will be here for this class
    }

    Here we have created same class Opencart in another namespace Demo .Now we can include both file in our application and easily we can differentiate and use  both class Opencart from both namespace. As i have given a example how we can use both class in same file

    <?php
    
    // include both of our Opencart classes 
    require "Demo/Opencart.php";
    require "Webkul/Opencart.php"; 
    
    // create a new instance for Opencart in the Demo namespace
     $Opencart1 = new Demo\Opencart(); 
     // create a new instance for Opencart in the Webkul namespace 
     $Opencart2 = new Webkul\Opencart();

    Both of these classes are different from each other and also  have different code , different functionality . But here we are using both and namespace allow us to  use same class name and differentiate them by there namespaces. Also we can make these code more readable by using PHP use function. For example, let’s say  we want to just use Opencart in place of  Demo\Opencart. Then by using PHP use function we can do like this

    Searching for an experienced
    Opencart Company ?
    Find out More
    <?php
    
     require "Demo/Opencart.php";
     require "Webkul/Opencart.php"; 
    // included both of our Opencart classes 
    
    use Demo\Opencart as Opencart;
    //this is to tell our code that i will use Opencart in place of Demo/Opencart
    
    //Now use of Opencart for Demo namespace
     $Opencart1 = new Opencart();

    We have used use PHP function to tell our code that i want use Opencart in place of writing Demo/Opencart. Now also we can use another namespace class for that  we have to do like this .

    //This will change the Opencart class from demo to webkul namespace
    
    use Webkul\Opencart as Opencart;
    // Now if you will create a instance for opencart class
    $Opencart2 = new Opencart(); 
    
    //This is new intance of class Opencart from Webkul namespace
    

    Now if you will use Opencart class then you are using it form Webkul namespace . and you can create instance which will belongs to Opencart class from Webkul namespace. Thats all from this blog.

     

     

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home