Home > Article > Backend Development > Detailed introduction to namespaces in PHP_PHP tutorial
This article mainly introduces the detailed introduction of namespaces in PHP. This article explains the concept of namespace (namespace), the naming in use Space, defining namespaces, sub-namespaces, calling code from namespaces, etc. Friends who need it can refer to it
Overview
PHP’s support for namespaces has gone through a difficult journey. Fortunately, PHP introduced namespaces starting with 5.3. Since the introduction of namespaces in PHP, the applicable structure of PHP code has also been greatly improved. Many programming languages have long had the concept of namespaces. Compared to other languages, PHP's support for namespaces is a little late. No matter what, the introduction of every new feature has its purpose. Like other languages, PHP introduces namespaces mainly to solve the problem of name conflicts.
The concept of namespace
The code is as follows:
When using namespace names in strings, be sure to escape
You can think of the namespace as a drawer. You can put pencils, rulers, A4 paper, etc. in the drawer. These are your own private items. Underneath your drawer is someone else's drawer, and someone else can put the same items in the drawer. In order not to take the wrong items, you decided to put labels on your drawers so that you can clearly see who belongs to an item.
Previously, developers had to add underscores to classes, functions, and constants to make their code independent of the code base. This is equivalent to everyone labeling their belongings and putting them into a larger drawer. Although this is a way to organize code, it is very inefficient.
The arrival of namespace is to solve this problem. We can declare the same functions, classes, and constants in different namespaces without causing name conflicts. In essence, namespaces are nothing more than a hierarchical way of marking PHP code.
Using namespace
One thing to note is that we are using namespaces indirectly. Starting from PHP 5.3, all declarations (classes, functions, constants) in non-user-defined namespaces belong to the global namespace by default.
The global namespace contains all PHP internal definitions, such as echo(), mysqli_connect() and Exception classes. Because the global namespace does not have an independent identifier, it is often called the global space.
Define namespace
The definition of the namespace must be the first statement of the PHP file. The only statement allowed before defining a namespace is the declare statement.
Defining a namespace is very simple, just use the keyword namespace. The name of the namespace needs to follow the naming rules of other identifiers in the PHP file.
The following is an example of defining a namespace:
The code is as follows:
namespace MyNamespace{
class Test{
}
}
If you want to define a code block that belongs to the global space, you also use the namespace keyword, but do not add the name of the namespace after it, as follows:
The code is as follows:
namespace {
class Test{
}
}
We can even define multiple namespaces in one file, as follows:
The code is as follows:
namespace MyNamespace {
}
namespace MySecondNamespace {
}
namespace {
}
We can also spread a namespace across different files, and the handlers included in the files will automatically merge them. Therefore, it is a good programming practice to limit a large number of namespaces to be defined in the same file, just as we usually define a separate file for each class separately.
Copy the code. The code is as follows:
One thing to note is that { containing the namespace code block is optional and can be used or not. In fact, as long as we insist on defining only one namespace in a file, we can completely omit {, which can also make our code look more concise.
Child namespace
Namespaces can follow a specific hierarchy, just like directories in our computer file system. Subnamespaces are particularly useful for structuring a project. For example, if your project needs to access a database, you may want to put all database-related code (such as database exception handling, etc.) in the same subdirectory.
To maintain flexibility, it is wise to place subnamespaces in subdirectories. This will make your code structure clearer and make it easier to use autoloaders that comply with the PSR-0 standard.
PHP uses backslash as namespace separator. Interestingly, PHP even considered using smiley face :) as namespace separator.
Example of sub namespace definition:
The code is as follows:
namespace MyProjectDatabase
class Connection {
}
You can use as many sub-namespaces as you like:
The code is as follows:
namespace MyProjectBlogAuthHandlerSocial;
class Twitter {
}
One thing to note is that PHP does not support nested definitions of namespaces. The following code will cause a fatal error: Namespace declarations cannot be nested.
The code is as follows:
namespace MyProject {
namespace Database {
class Connection { }
}
}
Calling code from a namespace
If you want to instantiate a class, call a function or use a constant in a different namespace, you need to use a backslash. They can be analyzed from three perspectives:
1. Unqualified name
2. Limited name
3. Fully qualified name
Unqualified Name
This is the name of a class, function or constant, but does not include any named references. If namespaces are new to you, this is the angle you're familiar with.
The code is as follows:
namespace MyProject;
class MyClass {
static function static_method()
{
echo 'Hello, world!';
}
}
// Unqualified name, resolves to the namespace you are currently in (MyProjectMyClass)
MyClass:static_method();
Qualified Name
This is how we use subnamespaces. An example is as follows:
The code is as follows:
namespace MyProject;
require 'myproject/database/connection.php';
// Qualified name, instantiating a class from a sub-namespace of MyProject
$connection = new DatabaseConnection();
Fully Qualified Name
The use of qualified names and unqualified names mentioned above are all relative to the current namespace. The above two methods can only be used to access the current namespace and deeper sub-namespaces.
If you want to access a namespace at a higher level than the previous namespace, you need to use a fully qualified name - an absolute path rather than a relative path. This boils down to adding a backslash at the very beginning of the namespace. Using a fully qualified name lets PHP know that the call is originating from the global space, rather than relative to the current namespace. An example is as follows:
The code is as follows:
namespace MyProjectDatabase;
require 'myproject/fileaccess/input.php';
// Trying to access the MyProjectFileAccessInput class
// This time it will work because we use the fully qualified name, note the leading backslash
$input = new MyProjectFileAccessInput();
We don’t need to use fully qualified names for PHP’s internal functions. If you call a class or function with an unqualified name that does not exist in the current namespace, PHP will search the global space.
With this rule in mind, we can rewrite PHP’s internal functions as follows:
The code is as follows:
namespace MyProject;
var_dump($query); // Overloaded
var_dump($query); // Internal
// We want to access the global Exception class
// The following will not work because there's no class called Exception in the MyProjectDatabase namespace and unqualified class names do not have a fallback to global space
// throw new Exception('Query failed!');
// Instead, we use a single backslash to indicate we want to resolve from global space
Throw new Exception('ailed!');
Function var_dump() {
echo 'Overloaded global var_dump()!
';
}
Dynamic call
PHP is a dynamic language, and this feature of PHP can also be used to call namespaces. This is essentially the same as instantiating a variable class and including a variable file. In strings, the namespace delimiter () used by PHP is also a metacharacter and therefore needs to be escaped.
The code is as follows:
namespace OtherProject;
$project_name = 'MyProject';
$package_name = 'Database';
$class_name = 'Connection';
// Include a variable file
require strtolower($project_name . '/' . $package_name . '/' . $class_name) . '.php';
// Name of a variable class in a variable namespace. Note how the backslash is escaped to use it properly
$fully_qualified_name = $project_name . '\' . $package_name . '\' . $class_name;
$connection = new $fully_qualified_name();
namespace keyword
The keyword namespace can not only be used to define a namespace, it can also be used to display the current namespace. Its function at this time is equivalent to the self keyword in the class.
The code is as follows:
namespace MyProject;
Function run()
{
echo 'Running from a namespace!';
}
// Resolves to MyProjectrun
run();
// Explicitly resolves to MyProjectrun
namespacerun();
__NAMESPACE__ constant
Just like the self keyword cannot represent the name of the current class, the namespace keyword cannot be used to represent the name of the current namespace. The __NAMESPACE__ keyword is used to solve this problem.
The code is as follows:
namespace MyProjectDatabase;
// 'MyProjectDatabase'
echo __NAMESPACE__;
This keyword is very useful for determining whether the current code starts from the namespace, and can also be used to debug the code.
Import or alias
Namespaces in PHP also support imports, and imports are also called aliases. Only classes, interfaces and namespaces can be imported (aliased). Importing is a very useful and basic function in namespaces. It allows us to use external code packages without worrying about name conflicts. The import function can be implemented using the use keyword. You can also use the as keyword to specify an alias when importing.
The code is as follows:
Use [name of class, interface or namespace] as [optional_custom_alias]
A fully qualified name can be replaced by an unqualified alias, so that we don’t have to use the fully qualified name every time it is used, thereby simplifying the code. Imports should be used at the top level of the namespace or in the global space. Using the import function within a function scope is illegal syntax.
The code is as follows:
namespace OtherProject;
// This holds the MyProjectDatabase namespace with a Connection class in it
require 'myproject/database/connection.php';
// If we want to access the database connection of MyProject, we need to use its fully qualified name as we're in a different name space
$connection = new MyProjectDatabaseConnection();
// Import the Connection class (it works exactly the same with interfaces)
Use MyProjectDatabaseConnection;
// Now this works too! Before the Connection class was aliased PHP would not have found an OtherProjectConnection class
$connection = new Connection();
// Import the MyProjectDatabase namespace
use MyProjectDatabase;
$connection = new DatabaseConnection()
We can simplify the above code by using aliases:
The code is as follows:
namespace OtherProject;
require 'myproject/database/connection.php';
Use MyProjectDatabaseConnection as MyConnection;
$connection = new MyConnection();
Use MyProjectDatabase as MyDatabase;
$connection = new MyDatabaseConnection();
Summary
Namespaces are used to avoid definition conflicts and introduce a more flexible and organized way to code. One thing to note is that we are not obliged to use namespaces. It is a working method used in combination with object-oriented. However, if we use namespaces, our code may reach a new level, and the quality will appear higher.