Preface: On Habré already published several articles on the namespace in PHP (all references to them I provide in the application). However, this interesting and useful question was not fully disclosed. Therefore, I cite the translation of the first of three articles on this topic (I will translate the rest in the near future). PS Article for beginnersNamespaces are one of the most significant changes in PHP 5.3. They will be familiar with C # and Java developers, and probably they will improve the structure of PHP applications.
')
Why do we need a namespace?
As the size of your PHP code library grows, the risk of accidentally overriding a function or class name that was previously declared increases. The problem is exacerbated when you try to add third-party components or plugins; What happens if two or more sets of code execute the Database or User classes?
Previously, the only solution was long class / function names. For example, WordPress added the “WP_” prefix to each name. Zend Framework usually gives detailed descriptions of the names, which leads to long class names, such as Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive.
Name match issues are resolved by introducing namespaces. PHP constants, classes and functions can be grouped into namespace libraries (namespaced libraries).
How are namespaces defined?
By default, all the names of constants, classes and functions are located in the global space - as it was before PHP began to support namespaces.
In the code, namespaces are defined using the single word
namespace at the very beginning of your PHP file. This word
must be the very first command (except for
declare ) and neither non-PHP code, nor HTML, nor even a space should precede this command, for example:
<?php
All code following these lines will be in the “MyProject” namespace. It is impossible to nest other namespaces in it, or define more than one namespace for the same part of the code (since only the last namespace declaration will be recognized, the previous ones are ignored). However, you can define different namespaces in the same file, for example:
<?php namespace MyProject1;
But, although this is possible, I would not advise doing this: be sensible when defining only one namespace for each file.
Subspaces of names (Sub-namespaces)
PHP allows you to define a hierarchy of namespaces so that libraries can be co-ordinated with each other. The resulting namespaces are separated by backslashes \, for example:
- MyProject \ SubName
- MyProject \ Database \ MySQL
- CompanyName \ MyProject \ Library \ Common \ Widget1
Calling Namespace Code
In the file named
lib1.php, we define a constant, a function, and a class within the
App \ Lib1 namespace :
lib1.php
<?php // application library 1 namespace App\Lib1; const MYCONST = 'App\Lib1\MYCONST'; function MyFunction() { return __FUNCTION__; } class MyClass { static function WhoAmI() { return __METHOD__; } } ?>
Now we can include this code in another PHP file, for example:
myapp.php
<?php header('Content-type: text/plain'); require_once('lib1.php'); echo \App\Lib1\MYCONST . "\n"; echo \App\Lib1\MyFunction() . "\n"; echo \App\Lib1\MyClass::WhoAmI() . "\n"; ?>
No namespaces are defined in myapp.php, so the code exists in the global space. Any direct link to
MYCONST ,
MyFunction, or
MyClass will fail because they only exist in the
App \ Lib1 namespace . To call the code from
lib1.php , we can add the prefix
\ App \ Lib1 to determine the full qualified name. On exit, when we
upload myapp.php , we get the following result:
App\Lib1\MYCONST App\Lib1\MyFunction App\Lib1\MyClass::WhoAmI
Full qualified names can become quite long; However, there are several obvious advantages in defining long class names, like
App-Lib1-MyClass . Therefore, in the next article, we will discuss the use of pseudonyms (aliasing) and understand how PHP resolves namespace names.
See also:
How to use namespaces in PHP, Part 2: Importing, aliases, and name resolution rulesHow to use namespaces in PHP, Part 3: Keywords and autoloadingNotes:
A / The only detailed article on the topic on Habré:
There are also references in the articles:
B / Remarks, amendments, indications of inaccuracies and so on. -
welcome !