Knownow
Knownow

Reputation: 385

Import namespace in PHP

I have just started learning a PHP framework and using namespace and use. I have gone through some resources trying to understand them (https://code.tutsplus.com/tutorials/namespacing-in-php--net-27203). I think I get what namespace is but I do not quite understand how namespace importing (use keyword) work.

I get that namespace works like below. I have a lib.php file:

<?php
namespace namespaceproj\lib1;
 class lib1 {
     private $var;
     function __construct(){
         $this->var='Run from lib';
         echo $this->var;
     }
 }

I have an index.php file

<?php
namespace main ;
include "lib.php";

class lib1 {
     private $var;
     function __construct(){
         $this->var='run from main';
         echo $this->var;
     }
}
$obj=new \namespaceproj\lib1\lib1(); // this refers to the class  with namespace namespaceproj\lib1;  
$obj = new lib1();// this refers to the class with namespace main .

All fine and good now I am trying to use the use keyword. I use the namespace\classname in the use keyword and can instantiate the class directly. I am guessing the namespace of the class becomes main when we use "use".

<?php
namespace main ;
include "lib.php";

class lib1 {
     private $var;
     function __construct(){
         $this->var='run from main';
         echo $this->var;
     }
}

//Accessing class using use
use namespaceproj\lib1\lib1 as lib1inc ;
$obj=new lib1(); //This refers to class with main namespace
$obj=new lib1inc(); // This refers to class with namespaceproj\lib1; namespace 
*/

Now is the part I am not able to understand. What happens if you do not want to import classes individually but import many classes in a namespace? The resource say you can use the use keyword to import namespaces. So I try to import the namespace of the lib file and try to instantiate the object. If I try something like this

use namespaceproj\lib1 ;
$obj=new libnew();

It gives me an error saying:

Cannot use namespaceproj\lib1 as lib1 because the name is already in use in C:\xampp\htdocs\AllTest\index.php on line 26

I was guessing the namespaces and all classes in that namespaces was getting imported to the main namespace and hence the error .

So I used the as keyword

use namespaceproj\lib1  as libnew;
$obj=new libnew();

This gives me an error:

Fatal error: Class 'namespaceproj\lib1' not found in C:\xampp\htdocs\AllTest\index.php on line 27.

My questions are:

  1. Does the use keyword always expect a class even though I tell it to import a namespace ?
  2. How do you import a namespace (and then instantiate multiple classes in that namespace) ?
  3. For the above example how do you use both the classes, one in main namespace and the other in namespaceproj\lib1 namespace ?

Upvotes: 0

Views: 1436

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35367

use namespaceproj\lib1 ;

Here you are importing the namespace, not all of the classes inside of that namespace. Therefore, you're trying to create a symbol lib1 pointing towards the namespace namespaceproj\lib1. This fails because you already have a lib1 symbol as a class.

use namespaceproj\lib1  as libnew;

Again, namespaceproj\lib1 is a namespace, not a class. So now you have a symbol named libnew for the namespace and you can instantiate classes using this aliased namespace:

new libnew\lib1;

PHP7 introduced group import syntax, but it still requires you to declare which classes you are importing.

use some\namespace\{ClassA, ClassB, ClassC}

Upvotes: 2

Related Questions