Today we will learn about a new concept called namespaces in PHP in this PHP Namespaces tutorial.
- Namespace is a new concept in PHP which was introduced in PHP version 5.3.
- Namespace is a container used to group classes, interfaces, constants, functions, etc.
- It is used for proper organization of the data and also to avoid naming conflicts.
- Till now we were unable to name two classes with same name in PHP, but we can make it possible using namespaces. Two different namespaces can have classes with same name without any conflict.
- In a large application, we can group related classes, interfaces, etc. in respective namespaces and use them accordingly whenever required.
- The classes in different namespaces are accessed with the respective namespace name.
- For example: if class first is defined in namespace ABC and another class with name first is defined in namespace EFG, then both the classes can be accessed as ABC\first and EFG\first.
- The languages like java, dotnet already have namespace concept in the form of packages and namespaces.
- We will learn how to use namespaces step by step.
- Defining a Namespace:
- To use a namespace, we first have to define it. To define a namespace, we have to use a keyword namespace followed by its name.
- The definition of namespace should be the first statement in a PHP file.
- The namespace name should follow the same rules as the name of any function or class. I mean, it can start with a letter or an underscore and can contain any number of letters, numbers etc.
- We can define namespaces in two ways:
- First way:
<?php namespace Web; //a class, function, etc. here ?>
<?php namespace Website { //a class, function, etc. here } ?>
<?php namespace { //code here } ?>
- We can have a namespace inside another namespace. You can imagine this just like a folder inside another folder and any hierarchy of such folders is allowed.
- Sub namespaces inside a namespace is defined by using a backslash between the namespaces.
- Let us see this example:
<?php namespace Website\hospital\medicine; //code here ?>
namespace Website\hospital\medicine;
- You can have multiple namespaces in a PHP file as shown below:
<?php namespace N1; // code for namespace N1 namespace N2; //code for namespace N2 Namespace N3; //code for namespace N3 ?>
- We can use the defined namespace in another program by calling them in 3 ways:
- Using unqualified name.
- Using qualified name.
- Using fully qualified name.
- Unqualified name: unqualified name means referring the file name in the current namespace. If we have a namespace Classes with a class C1 in it, we can refer it directly in the current namespace classes as $c=new C1(); this resolves to Classes\C1.
- Qualified name: qualified name means referring the file name in the sub-namespace of the current namespace. If we have a namespace Customer inside the namespace Service and we have a class Provide in namespace Customer and if we refer it from namespace Service as $s=new Customer\Provide(); this resolves to Service\Customer\Provide.
- Fully qualified name: fully qualified name means referring any file in any namespace. Here, if we want to refer the class Provide discussed about in above point in some another namespace, we can refer it as $p=Service\Customer\Provide(); this resolves to Service\Customer\Provide.
- Thus we can access any class, interface, etc. from or in any namespace by using its unqualified or qualified or fully qualified names.
- Let us see an example:
- To demonstrate the example, create a new folder named namespace in the htdocs folder in xampp folder. Now open a new notepad++ document and save it as app.php in the newly created namespace folder. Now write the following code in app.php file:
<?php namespace Service; class Customer { function hello($str="Customer") { echo "Hello ".$str; } } ?>
<?php require_once("app.php"); $c=new Service\Customer(); $c->hello(“Mary”); ?>
- Imagine that you have your Customer class in CallCenter\ServiceProvider\Service namespace.
- You would have to access this fully qualified name everywhere you would like to use the Customer class function.
- It becomes very annoying if you have to write such a long path every time.
- PHP has made it quite simple by using the use keyword.
- You just have to put the namespace name following the use keyword at the top and then use the class name directly.
- Write the following code in app.php file which includes sub-namespace as follows:
<?php namespace CallCenter\ServiceProvider\Service; class Customer { function hello($str="Customer") { echo "Hello ".$str; } } ?>
<?php require_once("app.php"); use CallCenter\ServiceProvider\Service\Customer; $c=new Customer(); $c->hello("Mary"); ?>
use CallCenter\ServiceProvider\Service\Customer;
$c=new Customer();
use CallCenter\ServiceProvider\Service;
we would have created the Customer object as shown below:
$c=new Service\Customer();
<?php require_once("app.php"); use CallCenter\ServiceProvider\Service as Care; $c=new Care\Customer(); $c->hello("Mary"); ?>
use CallCenter\ServiceProvider\Service as Care;
$c=new Care\Customer();
use CallCenter\ServiceProvider\Service, Employer\Employee;
Thus we successfully studied the concept of namespaces in this PHP Namespaces tutorial.