Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Namespaces

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 763
    Comment on it

    What is namespaces

    PHP introduce NAMESPACES in 5.3 version. Namespaces are virtual container/directory to organize code structure. In php we can not share same name for two classes. For example if you are using user management plugin for your project and this plugin contain User class for user verification. And also you have User class for user related activities in your application. If you include both file to access User class then PHP will throw error (Cannot redeclare class). To solve this problem we use Namespaces to organize or separate classes.

    Now let's start how to use Namespaces in PHP.

    Let's say you have user_plugin.php file which is plugin file need to include in your application:

    // user_plugin.php file

    class User
    {
        public function login()
        {
            echo 'Login successfully using plugin';
        }
    
        // .....
    }
    
    ?>
    

    And other php file is user_myapp.php in your application for user related activities:

    // user_myapp.php file

    <?php
    
    class User
    {
        public function register()
        {
            echo 'Registration successfully';
        }
    
        // .....
    }
    
    ?>
    

    Now if you want use both files (user_plugin.php and user_myapp.php ) in your index.php file.

    // index.php file

    <?php
    include 'user_plugin.php';
    include 'user_myapp.php';
    
    // calling login function of User class (From plugin file)
    $userPluginObj= new User();
    $userPluginObj->login();
    
    
    // calling register function of User class (From Application file)
    $userAppObj= new User();
    $userAppObj->register();
    
    ?>
    

    This code will throw PHP Fatal error: Cannot redeclare class User.

    Now to solve this we need to use Namespaces. How to use namespaces. Simple we need to add some line in our application file (user_myapp.php)

    <?php
    
    //-- Creating Namespase and adding virtual path to access User class (App\UserApp)
    
    namespace App\UserApp;
    
    class User
    {
        public function register()
        {
            echo 'Registration successfully';
        }
    
        // .....
    }
    ?>
    

    Now in index.php file you just need to change few lines to access Namespace:

    <?php
    
    include 'user_plugin.php';
    include 'user_myapp.php';
    
    use App\UserApp\User as myAppUser; // -- for Application User
    
    // calling register function of User class (From Application file)
    $userAppObj= new myAppUser();
    $userAppObj->register();
    
    // calling login function of User class (From pluging file)
    $userPluginObj= new User();
    $userPluginObj->login();
    
    
    ?>
    

    Thanks,

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: