PHP object-oriented programming and design patterns (5 ). PHP advanced programming learning notes 4.06.12 namespace overview PHP versions later than 5.3.0 start to support namespace. What is a namespace? In a broad sense, namespace is a PHP advanced programming learning note 2014.06.12
Namespace overview
PHP versions later than 5.3.0 support namespaces. What is a namespace? In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. In PHP, the namespace is used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications:
The PHP namespace provides a way to combine related classes, functions, and constants. The following is an example of PHP namespace syntax:
Define a namespace
Although any valid PHP code can be included in the namespace, only three types of code are affected by the namespace. they are classes, functions, and constants. The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. In addition, unlike other PHP language features, the same namespace can be defined in multiple files, that is, the content of the same namespace can be separated and stored in different files. Of course, you can also define multiple namespaces in the same file.
namespace MyProject;class MyClass{ #code...}
Define the sub-namespace:It has a similar relationship with directories and files. The PHP namespace also allows you to specify a hierarchical namespace name. Therefore, namespace names can be defined in different layers:
namespace MyProject\helper\http;class MyClass{ #code...}
Define multiple namespaces in the same file:There are two methods to declare multiple namespaces in the same file. However, in actual programming practices, it is not recommended to define the Dogo namespace in the same file. This method is mainly used to merge multiple PHP scripts into the same file. The first method is listed below.
namespace MyProject\helper\http;class MyClass{ #code...}namespace MyProject\helper\request;class MyClass{ #code...}
However, this method is strongly not recommended. you can refer to the following braces for definition:
namespace MyProject\helper\http;{ class MyClass { #code... }}namespace MyProject\helper\request;{ class MyClass { #code... }}
Use of elements in the PHP namespace
Before discussing how to use a namespace, you must understand how PHP knows the elements in the namespace to be used. Class names can be referenced in three ways:
Use namespace: Alias/import
Allowing alias reference or external fully qualified names is an important feature of a namespace. The PHP namespace supports two alias or import methods: use an alias for the class name or use an alias for the namespace name. In PHP, aliases are implemented through the use operator.
Note that PHP does not support importing functions or constants.
Namespace foo; use My \ Full \ Classname as Another; // The example below is the same as use My \ Full \ NSname as NSname use My \ Full \ NSname; // import a global class use \ ArrayObject;
Name resolution rules
Before describing the name resolution rules, let's take a look at some important definitions:
Name resolution follows the following rules:
Example
Tips for Learning notes 4.06.12 namespace overview PHP supports namespaces in versions later than 5.3.0. What is a namespace? In a broad sense, namespace is...