SoFunction
Updated on 2025-04-08

Detailed interpretation of PHP's namespace (II)

1: The use of namespace keyword and __NAMESPACE__ constant in namespace

PHP supports two abstract methods to access elements inside the current namespace, __NAMESPACE__ magic constants and namespace keywords.

The constant __NAMESPACE__ will store the name string of the current namespace. If it is currently a global non-namespace, it will save an empty string.

The keyword namespace can be used to explicitly access elements in the current namespace or subnamespace. It is equivalent to the self operator in the class. If it is not currently in the global environment, then explicitly accessing the globally limited elements.

__NAMESPACE__Instance:

<?php 
namespace Index\Name{ 
 var_dump(__NAMESPACE__); //Print string(10) "Index\Name" 
 function foo($classname){ 
  return __NAMESPACE__.'\\'.$classname; 
 } 
} 
 
namespace{ 
 var_dump(__NAMESPACE__); //Print string(0) "" 
 var_dump(Index\Name\foo('ceshi')); //Print string(16) "Index\Name\ceshi"} 
?>

namespace instance:

<?php 
namespace Index\Name\Index{ 
 function foo(){ 
  return 2; 
 } 
} 
 
namespace Index\Name{ 
 var_dump(namespace\Index\foo()); //Print 2 
 function foo(){ 
  return 1; 
 } 
} 
 
namespace{ 
 var_dump(namespace\Index\Name\foo()); //Print 1} 
?>

Two: Use alias/imports of the namespace

The namespace has the functions of allowing alias to be imported and imported. The namespace import requires the use keyword. If you also need to set alias, use and as are needed to cooperate.

1) Import support scope:

1: Use an alias for the class name
2: Use an alias for the interface name
3: Use an alias for the namespace
4: 5.6 or above php versions allow functions or constants to use alias.

2) Alias/import format

Import format: use [function/constant] Namespace fully qualified name [class/interface/function/constant]
Alias ​​import format: use [function/constant] namespace fully qualified name [class/interface/function/constant] as alias

Note: If you do not use a fully qualified name, it will become the current namespace + qualified name to combine the complete namespace. Therefore, if you do not write a fully qualified name, you must pay more attention to whether the combination result is the correct namespace.

Import instance:

<?php 
namespace Index\Col\Ads{ 
 
 const INSTANCE='const_val'; 
 
 function functionName(){ 
  return 'function_val'; 
 } 
 
 class className{ 
  static function classv(){ 
   return 'class_val'; 
  } 
 } 
} 
 
namespace Col{ 
 const INSTANCE='const_val_col'; 
 
 function functionName(){ 
  return 'function_val_col'; 
 } 
 
 class className{ 
  static function classv(){ 
   return 'class_val_col'; 
  } 
 } 
} 
 
 
namespace Index{ 
/*Introduce Index\Col\Ads namespace*/ 
use \Index\Col\Ads; 
/*Read the imported namespace constants*/ 
echo \Index\Col\Ads\INSTANCE.'<br/>'; //Print class_val/* Functions that read the imported namespace*/ 
echo \Index\Col\Ads\functionName().'<br/>'; //Print class_val/*Read the class of the introduced namespace, and the interface is the same*/ 
echo \Index\Col\Ads\className::classv().'<br/>'; //Print class_val 
/*Introduce constants*/ 
use const \Col\INSTANCE; 
/*Read constant*/ 
echo INSTANCE.'<br/>'; //Print const_val_col 
/*Introduce functions*/ 
use function \Col\functionName; 
/*Read function*/ 
echo functionName().'<br/>'; //Print function_val_col 
/*Introduce classes or interfaces*/ 
use \Col\className; 
/*Read class or interface*/ 
echo className::classv().'<br/>'; //Print classname_val_col 
} 
?> 

In the above example, the Index namespace is written with a fully qualified name. If Index\Col\Ads does not have the previous \global operator, it will become the Index\Index\Col\Ads namespace, so be sure to pay attention.

Alias ​​import instance:

<?php 
namespace Index\Col\Ads{ 
 
 const INSTANCE='const_val'; 
 
 const NS='namespace'; 
 
 function functionName(){ 
  return 'function_val'; 
 } 
 
 class className{ 
  static function classv(){ 
   return 'class_val'; 
  } 
 } 
} 
 
 
namespace{ 
/*Introduce the Index\Col\Ads namespace and set the alias Ads*/ 
use Index\Col\Ads as Ads; 
 
/*Introduce the constant INSTANCE of the Index\Col\Ads namespace and set the alias con*/ 
use const Index\Col\Ads\INSTANCE as con; 
 
/*Introduce the function functionName of the Index\Col\Ads namespace and set the alias func*/ 
use function Index\Col\Ads\functionName as func; 
 
/*Introduce class className of Index\Col\Ads namespace and set the alias classn. The alias of the interface is set the same as this*/ 
use Index\Col\Ads\className as classn; 
 
echo Ads\NS.'<br/>'; //Print namespaceecho con.'<br/>'; //Print const_valecho func().'<br/>'; //Print function_valecho classn::classv().'<br/>'; //Print class_val} 
?> 

In this example, there is a global non-namespace, so the imported namespace will not be affected without global operators.

Three: Special supplement

1: The first character of the namespace cannot be a number, it must be a letter or an underscore, otherwise a farse error will be reported.

2: The constants set by define in the namespace are global by default (exception: multiple namespaces in a file are wrapped in brackets. Define sets the constants of the namespace by default). Therefore, if a constant in the namespace is needed, it needs to be specifically stated in the constant name, such as define('Index\CON','CON') and define(__NAMESPACE__.'\CON','CON') and define(__NAMESPACE__.'\CON','CON') are both set constant CON in the namespace.

Constant instance:

<?php 
namespace Col; 
/*define default setting is global constant*/ 
define('CON','globals'); 
 
/*Special statement sets the constants under the current namespace*/ 
define(__NAMESPACE__.'\CON','col'); 
 
/*Special statement sets up constants under the Index namespace*/ 
define('Index\CON','index'); 
 
/*The global operator is directly followed by the constant name, so the global constant CON*/ 
var_dump(\CON); //globals 
 
/*No limit, so what is obtained is the constant CON of the current namespace*/ 
var_dump(CON); //col 
 
/*Global limitation, read the corresponding Col namespace constant CON*/ 
var_dump(\Col\CON); //col 
 
/*Global limitation, read the constant CON of its corresponding Index namespace*/ 
var_dump(\Index\CON); //index 
?> 

3: When you see the above example, it will be different when you refer to functions and classes (interfaces). The functions and classes (interfaces) set in the namespace are all contents of the namespace, regardless of whether they are set in multiple namespace braces in a file.

Examples of functions and classes: It can be seen that functions and classes in the namespace belong to the namespace

<?php 
function foo(){ 
 return 'global'; 
} 
 
class fool{ 
 static function ceshi(){ 
  return 'global'; 
 } 
} 
?>

&lt;?php 
namespace Col; 
require './'; //If the file is not introduced, the following \foo() and \foo::ceshi() will report fatal error 
function foo(){ 
 return 1; 
} 
 
class fool{ 
 static function ceshi(){ 
  return 2; 
 } 
} 
 
var_dump(\foo());   //global 
var_dump(foo());   //Print 1var_dump(\Col\foo());  //Print 1 
var_dump(\fool::ceshi());  //global 
var_dump(fool::ceshi());  //Print 2var_dump(\Col\fool::ceshi()); //Print 2?&gt; 

4: When setting the namespace, be careful not to use php keywords, such as function, class, abstract, etc., otherwise a parse error will be reported.

5: For the same namespace, no namespace is required for use between different files. You can just use functions, constants, classes and interfaces directly.

6: A class, constant, interface, and function in a namespace is introduced separately. If a function, constant, class, and interface conflict occurs, if no qualifier is used, the class, constant, interface, and function are introduced separately.

Example:

<?php 
namespace Lic; 
 
define(__NAMESPACE__.'\CON',1); 
 
function func(){ 
 echo 1; 
} 
 
class foo{ 
 static function ceshi(){ 
  return 1; 
 } 
} 

Only namespaces are introduced

&lt;?php 
namespace Col; 
require './'; 
use \Lic; 
 
define(__NAMESPACE__.'\CON',2); //The constants that set the namespace must state the namespace, otherwise they are global constants 
function func(){ 
 echo 2; 
} 
 
class foo{ 
 static function ceshi(){ 
  return 2; 
 } 
} 
 
var_dump(CON); //Print 2var_dump(namespace\CON); //Print 2 
func(); //Print 2namespace\func(); //Print 2 
var_dump(foo::ceshi()); //Print 2var_dump(namespace\foo::ceshi()); //Print2 

If classes, interfaces, functions, and constants are introduced separately, and names conflict and no limitations are used, the imported ones are preferred:

&lt;?php 
namespace Col; 
require './'; 
use \Lic\foo; 
use function \Lic\func; 
use const \Lic\CON; 
 
define(__NAMESPACE__.'\CON',2); //The constants that set the namespace must state the namespace, otherwise they are global constants 
function func(){ 
 echo 2; 
} 
 
class foo{ 
 static function ceshi(){ 
  return 2; 
 } 
} 
 
var_dump(CON); //Print 1var_dump(namespace\CON); //Print 2 
func(); //Print 1namespace\func(); //Print 2 
var_dump(foo::ceshi()); //Print 1var_dump(namespace\foo::ceshi()); //Print2 

This is the only timely addition, and there will be some subsequent additions in the future. . .