In the file system, we have directories or folders to keep related files. Similarly, PHP namespaces are used to have related code blocks of a PHP program that are reusable, like, classes, functions, interfaces under its reference.
And, PHP namespaces can also capable of containing constants.
With PHP namespaces, we can have the following operations listed below.
PHP namespaces are declared by namespace keyword followed by name of the namespace. And, importantly, this namespace declaration should be a very first line before writing any code to our PHP script. But, there is one exception, that is, it allows PHP declare statement before namespace declaration.
Following PHP code shows how to declare namespace contains PHP classes, functions, and constants.
<?php
namespace Database;
class DBController
{
public $database_name;
function __construct()
{
$this->database_name = "db_namespace";
}
}
function print_database_name($objDBController)
{
echo $objDBController->database_name;
}
?>
So, we have created a namespace named as Database, containing classes and functions to handle database related operations. Simply, we have a class with a constructor to initialize class properties, while creating objects, and we have a function under this namespace to print initialized value.
Since it is a namespaced class and function with the name of the class will not be treated as a constructor, we have used __construct() magic function. Otherwise, no initialization will be done for any object.
There are various ways in accessing namespaced classes, functions and etc. These are listed below.
This kind of access is very familiar as we have practiced on creating objects to access class members and invoking local functions. For example,
$objDBController = new DBController();
print_database_name($objDBController);
In the first line of above PHP code, we have created an object for the namespaces classes DBController(). Then, the PHP will search for Database\DBController, meaning that, it is searching for the class there exist under current namespace. So, we need not specify namespace’s name, like,
$objDBController = new Database\DBController();
Then, PHP will search for Database\Database\DBController and display class not found error, like,
Fatal error: Class 'Database\Database\DBController' not found...
For accessing from global space out of this namespace scope, we should add backslashes (\) before classes, functions or some name, what we want to access from global space. For example,
$objDBController = new \Database\DBController();
\Database\print_database_name($objDBController);
The above code will search for the namespaces Database and then specified classes and function under this namespace. So, the above code will create the same effect as that of normal access, without any class not found errors.
PHP namespace keyword is used to get the current namespace. So, we can specify required members of the current namespace to be associated with this keyword. And the code is,
$objDBController = new namespace\DBController();
namespace\print_database_name($objDBController);
This is similar to PHP namespace keyword for getting current namespace. For example,
__NAMESPACE__ . print_database_name($objDBController);
We should not add backslashes following this constant. Instead, we need to separate it by using PHP dot(.) Operator.
Hope this article will help to gain basic knowledge about PHP namespaces. But, This is only an introductory article. We will see more in future.
Download PHP Namespace Source Code