Name space
What is a namespace?
The namespace (English: Namespace) represents the visible range of identifiers (identifier). An identifier can be defined in more than one namespace, and its meaning in different namespaces is mutually irrelevant. In this way, any identifiers can be defined in a new namespace, and they do not conflict with any existing identifiers because existing definitions are in other namespaces.
Namespaces are simply meant to address naming conflicts that occur when different libraries are used in the same environment. For example, I have a function called a, but the system already has a function, then there will be a conflict.
Namespaces in PHP
PHP introduced namespaces starting with version 5.3, and many of the existing PHP class libraries and frameworks began to support it. So how does the PHP namespace work?
Define namespaces
Here is an example of defining a namespace
In the example above, a typical namespace definition method, only, const
is function
class
constrained by a namespace.
Using namespaces
The namespaces are used in the following ways:
The first thing to note is that the namespace is only a declaration, that is, when using a namespace, you still have to include the file that the namespace declares. In use, you can __NAMESPACE__
view the current namespace by using the.
See the official PHP documentation for more information
Auto Load
Each file is to declare the name of the control include
and manual is very intelligent things, so in their own PHP system or framework can use the automatic loading technology, let the system to find their own
The simplest way is to take advantage of function __autoload
functions, but this function can only be defined under a non-named control, that is, in the global context:
function __autoload ($class) { $dir = './'; Set_include_path (Get_include_path (). Path_separator. $ids _dir); $class = str_replace (' \ \ ', '/', $class). '. php '; Require_once ($class); }
If you are in a class that already has a namespace, you can use a function spl_autoload_register
to register a method in a class instead of__autoload
The above introduces the PHP namespace and automatic loading analysis, including the namespace, PHP content, I hope that the PHP tutorial interested in a friend helpful.