The PHP namespace (namespace) is one of the most important new features in PHP 5.3, a concept that has been in C # already, and namespace in PHP is the same as the C # concept.
I. Elements that dynamically access the namespace
namespace Me\poet;
function test ()
{
echo ' 1111 ';
}
$fun = ' test ';//cannot be used so that the last $fun () cannot dynamically invoke 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: Namespace name (output Me\poet)
//namespace operator: explicitly accessing elements in the current namespace or child namespace, equivalent to the self operator
\me\poet\test () in the class;
Namespace\test ();
The two lines of code are equivalent.
Three. Alias, import and global space (including multiple examples)
namespace Ws\weichen\www;
Use Ws\weichen\www as poet;//to define alias poet
//use ws\weichen\www;//without as, then take the last alias (WWW)
function demo ()
{
echo ' 1 ';
}
\ws\weichen\www\demo ();
Poet\demo ();
Www\demo (); Does not add as, then this calls the
The above three lines of code have the same effect.
The advantage of naming by rule (ws\weichen\www) is that if you change the domain name, you can change the prefix name so that 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 Call/
namespace a\b\c;
This function is A\b\c\fopen ();
function fopen ()
{
$f = \fopen (' demo.txt ');//Call Global fopen functions return
$f;
}