PHP Namespaces (namespace) dynamic access and usage tips, namespace namespace
PHP's namespace (namespace) is the most important new feature in PHP 5.3, a concept that has long been in C #, and the namespace in PHP is actually the same as the C # concept.
I. Dynamic access to elements of the namespace
Namespace Me\poet;function Test () { echo ' 1111 ';} $fun = ' test ';//cannot be used so that the last $fun () cannot be dynamically called to test (): Fatal error:call to undefined function test () $fun = ' \me\poet\test ';//correct//$ Fun = ' me\poet\test ';//correct $fun ();
That is, a dynamic call must be a qualified name or a fully qualified name (conceptual reference: the basis for using the PHP namespace)
Two. Magic Constants and operators
Namespace Me\poet;function Test () { echo ' 1 ';} Echo __namespace__; Magic constants: Name of the namespace (output Me\poet)//namespace operator: explicitly accesses elements in the current namespace or child namespace, equivalent to the self operator in the class \me\poet\test (); Namespace\test ();/ The last two lines of code are equivalent.
Three. aliases, imports, and global space (with multiple examples)
Namespace Ws\weichen\www;use ws\weichen\www as poet;//definition alias Poet//use ws\weichen\www; Do not add as, then take the last alias (WWW) function demo () { echo ' 1 ';} \ws\weichen\www\demo ();p Oet\demo ();//www\demo (); Does not add as, this invokes the
The above three lines of code effect the same.
The benefits of naming by rules (WS\WEICHEN\WWW): If you change the domain name, just change the name of the prefix, it does not affect the use of the alias www in the following code.
/* Import */include ' hello.class.php '; use \ws\weichen\www;use \hello;/*------------------------------------------------ --------*//* supports multiple use statements */use \nihao\shijie as Hello, \ws\weichen\www;/*--------------------------------------------- -----------*//* Global space: Backslash calls */namespace a\b\c;//this function is A\b\c\fopen (); functions fopen () { $f = \fopen (' demo.txt ');// Call the global fopen function return $f;}
Php namespace namespace error?
There's no grammatical problem.
What is your PHP version? PHP began supporting namespaces after version 5.3.0. Your PHP version may be lower.
PHP Use PHP namespace What's the matter?
1. Namespace Zend\http\phpenvironment;
This code defines a namespace that you can understand to define a domain name called zend\http\phpenvironment.
After the definition, the following class, interface, const, etc. are stated in the "domain" of the declaration. When referencing a containing file that declares a namespace, and wants to invoke something inside it, it must:
Adjust the current script also to this domain name, otherwise, you will have to use the full name of NAMESAPCE.
For example, inc.php file:
namespace Zend\http\phpenvironment;
Class Bar {}//defines a
When other files are called:
The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();
The second way to access Foo
namespace Foo; Adjust the current script to foo this NS domain, and namespace declaration must be in the first sentence
Require ' inc.php ';
$foo = new Bar ();
2. The Use keyword is used to alias the NS:
For example, the above
The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();
After using uses, the following wording is used:
Use \zend\http\phpenvironment as PE; Defining aliases
$foo = new \pe\bar (); Replace the original with a short alias.
If you omit the following as ..., then you can use the text in the last section instead, for example, above:
Use \zend\http\phpenvironment; Defining aliases
$foo = new \phpenvironment\bar (); Replace the original with a short alias.
================================================
Relevant content in the official PHP manual:
In PHP, the namespace namespace is used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:
1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.
The PHP namespace provides a way to group related classes, functions, and constants together.
The PHP namespace supports two ways of using aliases or imports: using aliases for class names, or aliases for namespace names, which are implemented using operator use. ... Remaining full text >>
http://www.bkjia.com/PHPjc/865621.html www.bkjia.com true http://www.bkjia.com/PHPjc/865621.html techarticle The dynamic access and use of PHP namespaces (namespace), Namespace namespace php namespace (namespace) is the most important new feature in PHP 5.3, a concept in C # that has ...