A preliminary discussion of the PHP namespace (Namespace), namespace Namespace
Explore the closure [view], and then explore the namespace.
For namespaces, the official documentation has been described in detail [view], and I've done a bit of practice and summary here.
Namespace one of the most explicit purposes is to solve the problem of duplicate names, PHP does not allow two functions or classes to appear the same name, otherwise it will produce a fatal error. In this case, as long as you avoid naming duplicates can be resolved, the most common practice is to contract a prefix.
Example: There are two modules in the project:article and message board, each of which has a class Commentthat handles user messages. then I might want to add some stats to all of my users ' messages, such as the number of messages I want to get. It is good practice to call them Comment the methods provided, but it is obviously not possible to introduce the respective Comment classes, and the code will go wrong, and rewriting any of the Comment in another place will also reduce maintainability. That's when the class name can only be refactored, and I've contracted a naming convention to precede the class name with the module name, like this:article_comment,messageboard_comment
As you can see, the name becomes very long, which means that more code (at least more characters) will be written later when using Comment . Furthermore, if you want to add more integration functions to each module, or call each other, the name will need to be reconstructed when the duplicate names occur. Of course, when the project began to notice the problem, and the naming rules can be very good to avoid the problem. Another workaround is to consider using namespaces.
Indicate:
Constants mentioned in this article: PHP5.3 the start of the const keyword can be used outside of the class. Both const and define are used to declare constants (their differences are not detailed), but in namespaces thedefine function is global and the Const acts on the current space. The constants I mentioned in this article refer to constants declared with const .
Basis
Namespaces divide code into different spaces (regions), constants, functions, and classes (for laziness, which I call elements below) do not affect each other, which is somewhat similar to the concept of ' encapsulation ' that we often refer to.
Creating a namespace requires the use of the namespace keyword, so that:
PHP// Create a namespace named ' article ' namespace article; >
Note that there cannot be any code in front of the first namespace of the current script file, and the following syntax is incorrect :
// example I//write some logic code
in front of the script php$path = "/"; class Comment {}namespace article;? >
// example two//output some characters
in front of the script Phpnamespace article;? >
Why do you say the first namespace? Because multiple namespaces can be created in the same script file.
Below I created two namespaces, and by the way I added a Comment class element to each of the two spaces:
PHP// Create a namespace named ' article ' namespace article; // this comment element that belongs to the article space class Comment {} // create a namespace named ' Messageboard ' namespace messageboard; // this comment element that belongs to the messageboard space class Comment {}?>
You cannot call other elements directly between different spaces, and you need to use the syntax of the namespace:
Phpnamespace article; class Comment {}namespace messageboard; class Comment {} // Call the comment class for the current space (messageboard) $comment New Comment (); // Call the comment class for article space $article _comment New \article\comment ();? >
As you can see, when you call the Comment class in the article space in the messageboard space, a syntax like file path is used: \ Space name \ element name
In addition to classes, the use of functions and constants is the same, I create new elements for two spaces, and output their values in messageboard space.
!--?
phpnamespace article;
Const PATH = '/article '
;
function
getcommenttotal () {
return +
;}
class
Comment {}namespace messageboard;
Const PATH = '/message_board '
;
function
getcommenttotal () {
return +
;}
class
Comment {}
//
constants, functions, and classes that call the current space
Echo PATH;
//
/message_board
echo getcommenttotal ();
//
+
$comment =
new
comment ();
//
constants, functions, and classes that call article space
echo \article\path;
//
/article
echo \article\getcommenttotal ();
//
$article _comment =
new
\article\comment (); ?
Then I did get the element data for the article space.
Sub-space
The invocation syntax of a namespace 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 actually in the same blog project. If you use namespaces to express their relationships, this is the case:
PHP// I use such a namespace to represent the article module under the Blog namespace blog\article; class Comment {} // I use such a namespace to represent the message board module under the blog namespace Blog\messageboard; class Comment {} // call the class of the current space $comment New Comment (); // class that calls 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 with some useful functions and classes:
PHPfunction getip () {}class filterxss {}?>
This script is introduced into a namespace, and the elements in the script do not belong to the namespace. If no other namespace is defined in this script, its elements are always in public space :
Phpnamespace blog\article; // Introducing script Files include './common_inc.php '; $filter _XSS New // Fatal error: BLOG\ARTICLE\FILTERXSS class not found $filter _XSS New // correct ?>
The way to call public space is directly before the element name plus \ on it, otherwise the PHP parser will think I want to invoke the element under the current space. In addition to the custom elements, including PHP's own elements, all belong to the public space.
To mention, in fact, the functions and constants of the public space do not have to add \ can also be normal call (do not understand why PHP to do so), but in order to correctly distinguish between the elements, or the recommendation to call the function when adding \
Name term
Before you say aliases and imports, you need to know the terminology about the space three names and how PHP parses them. The official documentation was very good, and I took it directly.
You can actually compare these three names to filenames (for example, comment.php), relative path names (for example, ./article/comment.php), Absolute pathname (for example, /blog/article/ comment.php), which may be easier to understand.
I used a few examples to represent them:
PHP// Create space blognamespace blog; class Comment {} // Unqualified name, indicates the current Blog space//This call will be parsed into blog\comment (); $blog _comment New Comment (); // The qualified name, relative to the Blog space//This call will be parsed into blog\article\comment (); $article _comment New //Class there is no backslash \//fully qualified name, that is absolute in the Blog space//This call will be parsed into blog\comment (); $article _comment New //Class The front has the backslash \//fully qualified name, indicates absolute in the Blog space//This call will be parsed into blog\article\comment (); $article _comment New //Class there's a backslash in front of the \//to create the blog space article namespace Blog\article; class Comment {}?>
In fact, I've been using unqualified names and fully qualified names before, and now they can finally call out their names.
Aliases and imports
Aliases and imports can be thought of as a quick way to invoke namespace elements. PHP does not support importing functions or constants.
They are all implemented by using the use operator:
Phpnamespace blog\article;classComment {}//Create a BBS space (I have plans to open a forum)namespace BBS;//Import a namespace Useblog\article;//you can invoke an element with a qualified name after importing the namespace$article _comment=Newarticle\comment ();//use aliases for namespaces UseBlog\article asArte;//use aliases instead of space names$article _comment=Newarte\comment ();//Import a class Useblog\article\comment;//you can invoke an element with an unqualified name after importing a class$article _comment=NewComment ();//using aliases for classes UseBlog\article\comment asCOMT;//use aliases instead of space names$article _comment=NewComt ();?>
I've noticed what happens if the element is imported with the same name element in the current space? It is obvious that a fatal error will occur.
Cases:
Phpnamespace blog\article; class Comment {}namespace BBS; class Comment {} Class COMT {} // Import a class Use blog\article\comment; $article _comment New // conflicts with the comment of the current space, the program generates fatal errors//uses aliases for classes Use as COMT; $article _comment New // conflicts with the COMT of the current space, the program generates fatal errors ?>
Dynamic invocation
PHP provides dynamic access elements for the namespace keyword and __namespace__ Magic constants,__namespace__ can be dynamically accessed by combining strings:
Phpnamespace blog\article; Const PATH = '/blog/article '; class Comment {} // The namespace keyword represents the current space Echo // /blog/article $comment New namespace\comment (); // the value of the magic constant __namespace__ is the current space name Echo // blog\article//can be combined into strings and called $comment _class_name = __namespace__. ' \comment '; $comment New $comment _class_name ();? >
Question of string form call
In the above example of dynamic invocation, we see a dynamic invocation in the form of a string, and there are two issues to note if you want to use this method.
1. Special characters may be escaped when using double quotation marks
Phpnamespace blog\article; class name {} // I was trying to call Blog\article\name . $class _name // but \ nthe will be escaped as a newline character. $name New $class _name // fatal error occurred ?>
2. Does not consider a qualified name
PHP compiles the script to determine the space in which the element resides, as well as the import situation. When parsing a script, a string invocation can only be considered a unqualified name and a fully qualified name, and it never could be a qualified name.
Phpnamespace Blog; // Import Common class Use Blog\article\common; // I want to call 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: Common class does not exist//I want to call Blog\article\common with qualified name $common _class_name = ' Article\common '; // It will actually be treated as a fully qualified name, and it will represent the common class under the article space, but I have defined only blog\article space, not article space . $common New $common _class_name // fatal error occurred: Article\common class does not exist namespace Blog\article; class Common {}?>
Summarize
I'm just in touch with PHP's namespace, and I can't give you some advice that doesn't make any practice. I personally think that the role and function of the namespace is very powerful, if you want to write plug-ins or general-purpose library can no longer worry about the name problem. However, if the project to a certain extent, through the addition of namespaces to solve the problem of duplicate names, I think the workload will not be less specific. Also have to admit that its syntax adds a certain degree of complexity to the project, so it should be well planned from the start of the project, and a naming convention should be developed.
http://www.bkjia.com/PHPjc/1129454.html www.bkjia.com true http://www.bkjia.com/PHPjc/1129454.html techarticle a discussion of PHP namespaces (Namespace), namespaces Namespace explore closures [view], and then explore namespaces. For namespaces, the official documentation has been described in detail [view], here I am ...