The PHP namespace (namespace) is php5.3. This concept is already very early in C #, PHP in the namespace in fact and C # concept is the same.
Why do you use namespace in PHP?
Suppose that if you do not use namespace, the name of each class in a project must be fixed. Because when PHP is new, whether it calls AutoLoad or invokes a loaded class, there is a file for the class name. So in the absence of namespace, we will think of various naming conventions to distinguish between different classes, such as Project1_school1_class1_student or project2_school_class_student.
After the introduction of the namespace can be effectively circumvented, a namespace is equivalent to a file path, to find this class, it will go to the corresponding file path to find the class definition file.
Indicate:
Constants mentioned in this article: the PHP5.3 start const keyword can be used outside of a class. Both const and define are used to declare constants (their differences are not detailed), but in namespaces the role of the define is global, while the const acts on the current space. The constants I refer to in the article refer to constants declared using Const.
Basis
Namespaces divide code into different spaces (regions), constants, functions, classes in each space (to be lazy, what I call the elements below) do not affect each other, a bit like the "encapsulation" concept we often refer to.
Definition and use of namespace
Defined:
<?php
namespace Myproject;
Or
<?php
Namespace Myproject {
}
Use:
<?php
Use Myproject/school;
<?php
Use Myproject/schoolas School1; Alias
The namespace is parsed at runtime. Use is equivalent to a declaration that does not parse and load. For example, the following example:
test.php
<?php
Use My\name;
Require_once ("/home/yejianfeng/handcode/test/namespace1.php");
$a =new my\name\a ();
$a->print1 ();
namespace1.php
<?php
namespace My\name;
Class A {
Public Function Print1 () {
Echo 11;
}
}
Although require_once is available under use, it is also normal to run because the program only loads the namespace when new My\name\a () is in place My\name
Global classes and Namespace classes
If you want to new a global class using the new \a ()
If you want to new a namespace class, use the new My\namespace\a ()
The Order of namespaces
Since you have a namespace, the most error-prone thing to do is to use the class, the search path for this class.
If we can figure out this example in manual, we can all figure out the search order.
<?php
namespace A;
Use b\d, C\eas F;
Function call
Foo (); First attempt to invoke function foo () defined in the namespace "a"
Try calling global function "foo" again
\foo (); Call Global space function "Foo"
My\foo (); Call definition function "foo" in Namespace "A\my"
F (); First try to invoke the function "F" defined in the namespace "a"
Try calling the global function "F" again
Class reference
New B (); Creates an object of class "B" defined in the namespace "a"
If not found, try to mount the class "a\b" Automatically
New D (); Use an import rule to create an object of class "D" defined in the namespace "B"
If not found, try to mount the class "b\d" Automatically
New F (); Use an import rule to create an object of class "E" defined in the namespace "C"
If not found, try to mount the class "C\e" Automatically
New \b (); To create an object that defines the class "B" in the global space
If not found, try to load the class "B" Automatically
New \d (); To create an object that defines the class "D" in the global space
If not found, try to mount the class "D" Automatically
New \f (); To create an object that defines the class "F" in global space
If not found, try to mount the class "F" Automatically
Call a static method or a namespace function in another namespace
B\foo (); Invoke the function "foo" in the namespace "a\b"
B::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "A"
If the class "a\b" is not found, attempt to automatically mount the class "a\b"
D::foo (); Use the import rule to invoke the "Foo" method of the class "D" defined in the namespace "B"
If the class "b\d" is not found, try to automatically mount the class "b\d"
\b\foo (); Call function "Foo" in Namespace "B"
\b::foo (); Call the "Foo" Method of Class "B" in global space
If class "B" is not found, try to mount the class "B" automatically
static methods or functions in the current namespace
A\b::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "A\a"
If the class "a\a\b" is not found, try to automatically mount the class "a\a\b"
\a\b::foo (); Invoke the "Foo" method of the class "B" defined in the namespace "a\b"
If the class "a\b" is not found, try to automatically mount the class "a\b"
?>
Creating a namespace requires the use of the namespace keyword, so that:
<?php
Create a namespace named ' Article '
namespace Article;
?>
Note that there is no code in front of the first namespace of the current script file, and the following is the wrong syntax:
Example One
Wrote some logical code in front of the script
<?php
$path = "/";
Class Comment {}
namespace Article;
?>
Case II
Some characters were printed before the script
<?php
namespace Article;
?>
Why do you say the first namespace? Because multiple namespaces can be created in the same script file.
I created two namespaces below, and added a comment class element to each of these two spaces:
<?php
Create a namespace named ' Article '
namespace Article;
This comment is an element of article space
Class Comment {}
Create a namespace named ' Messageboard '
namespace Messageboard;
This comment is an element of messageboard space
Class Comment {}
?>
You cannot call other elements directly between different spaces, and you need to use the syntax of the namespace:
<?php
namespace Article;
Class Comment {}
namespace Messageboard;
Class Comment {}
Call the comment class for the current space (messageboard)
$comment = new comment ();
Call comment class for article space
$article _comment = new \article\comment ();
?>
You can see that when you call the comment class in article space in messageboard space, you use a syntax like a file path: \ space name \ element name
In addition to classes, the use of functions and constants is the same, I created new elements for two spaces, and output their values in the messageboard space.
<?php
namespace Article;
Const PATH = '/article ';
function Getcommenttotal () {
return 100;
}
Class Comment {}
namespace Messageboard;
Const PATH = '/message_board ';
function Getcommenttotal () {
return 300;
}
Class Comment {}
constants, functions, and classes that call the current space
Echo PATH; Message_board
Echo Getcommenttotal (); 300
$comment = new comment ();
Calling constants, functions, and classes in article space
Echo \article\path; Article
Echo \article\getcommenttotal (); 100
$article _comment = new \article\comment ();
?>
Then I did get the element data for the article space.
Child space
The invocation syntax of namespaces makes sense as a file path, allowing us to customize the subspace to describe the relationships between the spaces.
Sorry I forgot to say, article and message board these two modules are in fact in the same blog project. If you use namespaces to express their relationships, this is true:
<?php
I use this namespace to represent the article module under the blog
namespace Blog\article;
Class Comment {}
I use such namespaces to represent the message board module under the blog
namespace Blog\messageboard;
Class Comment {}
Class that calls the current space
$comment = new comment ();
Calling a class of blog\article space
$article _comment = new \blog\article\comment ();
?>
Also, subspace can define many levels, such as Blog\article\archives\date
Public space
I have a common_inc.php script file that has some handy functions and classes:
<?php
function GetIP () {}
Class Filterxss {}
?>
This script is introduced into a namespace, and the elements in the script do not belong to that namespace. If no other namespace is defined in this script, its elements are always in the public space:
<?php
namespace Blog\article;
Introducing script files
Include './common_inc.php ';
$filter _XSS = new FILTERXSS (); Fatal error occurred: BLOG\ARTICLE\FILTERXSS class not found
$filter _XSS = new \FILTERXSS (); That's right
?>
The way to call a public space is to add it directly to the element name, or else the PHP parser will think I want to invoke the element in the current space. In addition to the custom element, also includes the PHP to bring the element, all belongs to the public space.
To mention, the functions and constants of the public space can be called normally without adding \ (I don't understand why PHP does this), but in order to properly differentiate between elements, it is recommended that you call the function by adding \
Name terminology
Before you say alias and import, you need to know the terms for space three names and how PHP parses them. The official document is very good and I'll just get it.
1. Unqualified name, or class name that does not contain a prefix, such as $comment = new Comment ();. If the current namespace is blog\article,comment, it will be resolved to blog\article\comment. If the code that uses comment is not contained in code in any namespace (in global space), then comment is resolved to comment.
2. A qualified name, or a name that contains a prefix, such as $comment = new Article\comment ();. If the current namespace is a blog, then Comment is resolved to blog\article\comment. If the code that uses comment is not contained in code in any namespace (in global space), then comment is resolved to comment.
3. Fully qualified name, or contains the name of the global prefix operator, such as $comment = new \article\comment ();. In this case, Comment is always resolved to the literal name (literal name) article\comment in the code.
You can actually compare these three names to file names (such as comment.php), relative path names (such as./article/comment.php), Absolute pathname (for example,/blog/article/comment.php), which may be easier to understand.
I have used several examples to represent them:
<?php
Create a Space blog
namespace Blog;
Class Comment {}
Unqualified name that represents the current blog space
This call will be parsed into blog\comment ();
$blog _comment = new comment ();
Qualified names, representing the relative to the blog space
This call will be parsed into blog\article\comment ();
$article _comment = new Article\comment (); Class is not preceded by a backslash \
Fully qualified name, representing absolute in blog space
This call will be parsed into blog\comment ();
$article _comment = new \blog\comment (); Class is preceded by a back ramp bar \
Fully qualified name, representing absolute in blog space
This call will be parsed into blog\article\comment ();
$article _comment = new \blog\article\comment (); Class is preceded by a back ramp bar \
Create a child space for a blog article
namespace Blog\article;
Class Comment {}
?>
In fact, I've been using unqualified names and fully qualified names, and now they can finally name them.
Aliases and imports
Aliases and imports can be seen as a quick way to invoke namespace elements. PHP does not support import functions or constants.
They are implemented by using the use operator:
<?php
namespace Blog\article;
Class Comment {}
Create a BBS space (I have a plan to open a forum)
namespace BBS;
Import a Namespace
Use blog\article;
To invoke an element with a qualified name after importing the namespace
$article _comment = new Article\comment ();
Use aliases for namespaces
Use blog\article as Arte;
Use aliases instead of space names
$article _comment = new Arte\comment ();
Import a class
Use blog\article\comment;
You can invoke an element with an unqualified name after importing a class
$article _comment = new comment ();
Using Aliases for classes
Use blog\article\comment as COMT;
Use aliases instead of space names
$article _comment = new COMT ();
?>
I noticed that if you were importing elements, what would happen if the current space had the same name element? Obviously, the result will be a fatal error.
Cases:
<?php
namespace Blog\article;
Class Comment {}
namespace BBS;
Class Comment {}
Class COMT {}
Import a class
Use blog\article\comment;
$article _comment = new comment (); Comment Conflict with current space, program generates fatal error
Using Aliases for classes
Use blog\article\comment as COMT;
$article _comment = new COMT (); COMT conflict with current space, program generates fatal error
?>
Dynamic invocation
PHP provides dynamic access to NAMESPACE keywords and __namespace__ Magic constants, __namespace__ can be dynamically accessed by combining strings:
<?php
namespace Blog\article;
Const PATH = '/blog/article ';
Class Comment {}
The namespace keyword represents the current space
Echo Namespace\path; Blog/article
$comment = new Namespace\comment ();
Magic constant __namespace__ value is the current space name
Echo __namespace__; Blog\article
Can be grouped into strings and called
$comment _class_name = __namespace__. ' \comment ';
$comment = new $comment _class_name ();
?>
The problem is called in string form
In the example of the dynamic invocation above, we see a dynamic invocation of the string as a way to pay attention to two problems if you want to use this method.
1. Special characters may be escaped when using double quotes
<?php
namespace Blog\article;
Class Name {}
I was trying to call Blog\article\name.
$class _name = __namespace__. "\name"; But \ nthe will be escaped as a newline character.
$name = new $class _name (); Fatal error occurred
?>
2. Not considered a qualified name
PHP determines the space in which the elements are located and how they are imported when the script is compiled. When parsing a script, a string invocation can only be considered a unqualified name and a fully qualified name, and can never be a qualified name.
<?php
namespace Blog;
Import Common class
Use Blog\article\common;
I want to invoke Blog\article\common with an unqualified name
$common _class_name = ' common ';
is actually treated as an unqualified name and represents the common class of the current space, but my current class does not create a common class
$common = new $common _class_name (); Fatal error occurred: Common class does not exist
I want to invoke Blog\article\common with a qualified name
$common _class_name = ' Article\common ';
is actually treated as a fully qualified name and represents the common class under the article space, but I only define the blog\article space instead of the article space below
$common = new $common _class_name (); Fatal error occurred: Article\common class does not exist
namespace Blog\article;
Class Common {}
?>
Summarize
I have just contacted the namespace of PHP and can't give some suggestions without practice. I personally think that the role and function of the namespace is very powerful, if you want to write plug-ins or general library when no longer need to worry about the name of the problem. However, if the project is to a certain extent, by adding namespaces to solve the duplicate name problem, I feel that the workload will not be the proportion of small names. Also have to admit that its syntax will add a certain degree of complexity to the project, so from the beginning of the project should be good planning it, and develop a naming convention.