Web Programming TutorialsAliasing of PHP Namespaces

Aliasing of PHP Namespaces

We learned about namespaces in the last tutorial, today we will learn aliasing of the namespace in this Aliasing of PHP Namespaces tutorial.

  • The ability to refer to an external fully qualified name with an alias is called aliasing or importing.
  • Aliases allow the user to reference a long namespace name with a shorter name.
  • Let us first see the behavior of the use keyword in a program.
  • We will define 2 files with two different namespaces and import them in another program.
  • To demonstrate this, create a new folder named namespaces in the htdocs folder in xampp folder. Now open a new notepad++ document and save it as app1.php in the newly created namespaces folder.
  • Now write the following code in app1.php file:
  • <?php
    	namespace App\Lib1;
    	
    	const CON='This is App\Lib1\CON';
    	
    	function fun()
    	{
    		return "This is ".__FUNCTION__;
    	}
    	
    	class call
    	{
    		static function receive()
    		{
    			return "This is ".__METHOD__;
    		}
    	}
    ?>
    
  • In the above code, we have defined a namespace App\Lib1; and so the following code belongs to App\Lib1 namespace.
  • We have defined a constant CON, a function fun() and a class call with a static function receive().
  • The function fun() returns __FUNCTION__. This is an inbuilt constant which returns the fully qualified name of the function from which it is returned.
  • Similarly the method receive() of class call returns __METHOD__. This is also a constant which returns the fully qualified name of the class and the method name from which it is returned.
  • Let us now have a look on code in another file app2.php. The code is given below:
  • <?php
    	namespace App\Lib2;
    	
    	const CON='This is App\Lib2\CON';
    	
    	function fun()
    	{
    		return "This is ".__FUNCTION__;
    	}
    	
    	class call
    	{
    		static function receive()
    		{
    			return "This is ".__METHOD__;
    		}
    	}
    ?>
    
  • The above code is same as in app1.php file. The only difference is that it is under App\Lib2 namespace.
  • Now it’s time to access the data in app1.php and app2.php in another file.
  • But before that we will have a look on the ways of accessing it.
  • To access data from a function, class, etc. we need to use its fully qualified name or qualified name or unqualified name.
  • Example of unqualified name: Suppose you are accessing a function in same namespace, you can use unqualified name. I mean if you have defined a function F1 in file A1 in namespace N1. Now you want to access the function F1 from file A1 in file A2 which is also under namespace N1, you can directly call it as F1(). It is an unqualified name of function F1(). This means an unqualified name is the name accessed in the current namespace.
  • Example of qualified name: Suppose you are accessing a function in sub-namespace from the root namespace, you can use qualified name. I mean if you have a function F1 defined in namespace N1\N2. And now you want to access it in namespace N1, then you can call it as N2\F1().
  • Example of fully qualified name: the fully qualified name is the full path of the function and it can be used to access the function, class etc. from anywhere outside the namespace. Suppose you have a function F1 in namespace N1\Ns1 in file A1. You want to access it in namespace N2 in file A2, you can call it as N1\Ns1\F1().
  • Now let us access the data in app1.php and app2.php in index.php file. The code in index.php is given below:
  • <?php
    	namespace App\Lib1;
    	
    	require_once("app1.php");
    	require_once("app2.php");
    	
    	echo "<ul>";
    	echo "<li>".CON."</li><br>";
    	echo "<li>".fun()."</li><br>";
    	echo "<li>".call::receive()."</li><br>";
    	echo "</ul>";
    ?>
    
  • We have a namespace App\Lib1; defined in the above code. So the whole following code belongs to namespace App\Lib1.
  • We have linked our index.php file to app1.php and app2.php file using require_once() function.
  • Next we have accessed the CON, fun() and receive() in an unordered list.
  • Here we have used unqualified names for the constant and functions. So let us see what happens. The output is shown below:
  • output1
    fig 1

  • Here we can see that the constant CON, the function fun() and the method receive() of class call contained in namespace App\Lib1 are displayed.
  • This was possible because we are accessing it with unqualified names within the same App\Lib1 namespace where these functions and constants are defined.
  • Now imagine that you have defined namespace App\Lib2; in index.php instead of namespace App\Lib1; You will get the output as follows:
  • output2
    fig 2

  • To call the element in current namespace we can also use namespace keyword.
  • For example: to call the method receive() in class call, we can write it like this:
  • $o=new namespace\call();
    $o->receive();
  • And to call the function fun() you can write it like this:
  • echo namespace\fun(); 
  • Now let us access the elements in above app1.php and app2.php in another program that has some other namespace or a global namespace i.e. no namespace. This is called importing namespace.
  • To access/import data in such situations the keyword use is used to specify the desired namespace name.
  • Let us write the program that accesses data from app1.php and app2.php files:
  • For this comment the code present in index.php file and write the following code below it:
  • <?php
    use App\Lib1;
    	
    	require_once("app1.php");
    	require_once("app2.php");
    	
    	echo "<br><ul>";
    	echo "<li>".Lib1\CON."</li><br>";
    	echo "<li>".Lib1\fun()."</li><br>";
    	echo "</ul>";
    ?>
    
  • Here we have used the statement use App\Lib1; This allows us to use the functions, classes, interfaces, constants, etc in namespace App\Lib1.
  • In this program we cannot use unqualified name to access the elements, because now we are not in the same namespace where the elements are defined.
  • We have used qualified names like Lib1\CON and Lib1\fun() here, because we have specified its complete namespace using the use keyword.
  • I mean we can call it like this also:
  • echo “<li>”.App\Lib1\CON.”</li><br>”;
    echo “<li>”.App\Lib1\fun().”</li><br>”;
  • But like this, we need to write the fully qualified name every time to access anything from another namespace.
  • If the namespace is very big it will become very tedious and annoying to write such a big namespace every time. To avoid this problem the use keyword is provided which allows us to write the complete namespace at the top just once and then use its qualified name later in the whole program.
  • Output of the above code is shown below:
  • output3
    fig 3

  • You can even specify the class you want to use in the program in the use statement. For example, you want to use the class call from App\Lib2 namespace. You can write the use statement as follows:
  • use App\Lib2\call;
  • You can have any number of use statements in a program or can specify number of namespaces in a single use statement separated by commas.
  • Aliasing a namespace:
    • We saw the use statement that allows us to write the complete namespace once and then use only the qualified name to access its elements.
    • But we can simplify it more by giving the namespace a shorter name which will be used instead of it in the program.
    • This is done with conjunction of keyword as with use.
    • Let us access the elements of namespaces App\Lib1 and App\Lib2 in index.php file. Comment the previous code and write the following new code there:
    • <?php
      namespace N1;
      		
      	use App\Lib1 as Lib;
      	use App\Lib2\call as O;
      	
      	require_once("app1.php");
      	require_once("app2.php");
      	
      	echo "<br><ul>";
      	echo "<li>".Lib\CON."</li><br>";
      	echo "<li>".Lib\fun()."</li><br>";
      	echo "<li>".Lib\call::receive()."</li><br>";
      	echo "<li>".O::receive()."</li><br>";
      	echo "</ul>";
      ?>
      
    • We have defined a namespace N1 in the above code. Hence the whole code belongs to namespace N1.
    • Here, we have imported the App\Lib1 namespace and the class call from the App\Lib2 namespace using the use statement.
    • The App\Lib1 namespace is named as alias Lib and App\Lib2\call is named as alias O.
    • Now the elements from App\Lib1 will be accessed using Lib and the elements from class call of App\Lib2 will be accessed using O.
    • This is called aliasing of namespace.
    • The constant CON, function fun() and method of calls call i.e. receive() of App\Lib1 namespace are accessed using Lib and the method of class call of App\Lib2 namespace are accessed using O in the above code.
    • The output is shown below:
    • output4
      fig 4

    • In the same way if we want to use some class which is not in any namespace, we can use it by going into the root namespace using a backslash (\) before the class name.
    • For example if you want to use DateTime class in the above program. You know that DateTime class is not in the namespace N1, it is outside namespace N1. So to come out of namespace N1 backslash is used before the class name as shown below:
    • $dt=new \DateTime();
    • This can also be written as:
    • use \DateTime();
      $dt=new DateTime();
      

Thus we learned how to deal with different namespaces while working in a project in this Aliasing of PHP Namespaces tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -