In 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.
Operations Performed using PHP NameSpaces
With PHP namespaces, we can have the following operations listed below.
- Grouping related PHP code blocks into one scope or space to refer in future.
- Creating functions or classes with the same name that already exist with user space or internal code structure without name clashes.
- Getting current namespace with appropriate PHP keyword or constants.
- Importing external classes, interfaces and namespaces with alias.
- Aliasing will avoid name clashes and for shortening the name of imported blocks for easy readability.
- Aliasing or importing are not applicable for importing external functions.
- Accessing classes, functions and etc., from global space.
Declaring PHP Namespace
PHP namespaces are declared by namespace keyword followed by name of the namespace. And, importantly, this namespace declaration should be 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 of PHP classes, functions and constants.
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 constructor to initialize class properties, while creating objects, and we have a function under this name space to print initialized value.
Since, it is an 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.
Accessing Namespaced Classes and Functions in PHP
There are various ways in accessing namespaced classes, functions and etc. These are listed below.
- Normal Access
- Accessing from global space
- Accessing with namespace keyword
- Accessing with __NAMESPACE__ keyword
Normal Access
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 classe 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...
Accessing from Global Space
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 same effect as that of normal access, without any class not found errors.
Accessing with namespace Keyword
PHP namespace keyword is used to get the current namespace. So, we can specify required members of current namespace to be associated with this keyword. And the code is,
$objDBController = new namespace\DBController(); namespace\print_database_name($objDBController);
Accessing with namespace Keyword
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.
0 comments:
Post a Comment