Detailed interpretation of PHP namespaces

Detailed interpretation of PHP namespaces

PHP Training

A: namespace using the namespace keyword and the constant __NAMESPACE__

PHP supports two ways to access the current namespace abstract interior elements, __ NAMESPACE__ Magic constants and the namespace keyword.

Constant __NAMESPACE__ will save the current namespace name string, if the current is a global non-namespace, you save is an empty string.

Namespace keyword can be used to explicitly access the current namespace name or sub-element space. Which is equivalent to a class of self operator. If you are not in the current global environment, then that is the global element defined explicit access.

__NAMESPACE__ example:

<?php

namespace Index\Name{

var_dump(__NAMESPACE__); //打印string(10) "Index\Name"

function foo($classname){

return __NAMESPACE__.'\\'.$classname;

}

}

namespace{

var_dump (__ NAMESPACE__); // print string (0) ""

var_dump(Index\Name\foo('ceshi')); //打印string(16) "Index\Name\ceshi"

}

?>

namespace Example:

<?php

namespace Index\Name\Index{

function foo(){

return 2;

}

}

namespace Index\Name{

var_dump (namespace \ Index \ foo ()); // prints 2

function foo(){

return 1;

}

}

namespace{

var_dump(namespace\Index\Name\foo()); //打印1

}

?>

II: Use namespace alias / Import

Namespace includes aliases is introduced and the introduction of features, the need to import the namespace using the use keyword, if you need to set the alias were blended with the required use as.

1) introducing the supported range:

1: Use an alias for the class name

2: Use an alias for the interface name

3: Use an alias for a namespace

4: more than 5.6 php version, allows functions or constants to use aliases.

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 the fully qualified name, then, too, and before using namespaces, as will become current namespace + qualified name to combine the full namespace, so if you do not write the fully qualified name, here we must pay more attention to the combination of whether the results are correct namespace.

Introduction example:

<?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{

/ * The introduction of Index \ Col \ Ads namespace * /

use \Index\Col\Ads;

/ * Read a constant namespace introduced * /

echo \Index\Col\Ads\INSTANCE.'<br/>'; //打印class_val

/ * Read function namespace introduced * /

echo \Index\Col\Ads\functionName().'<br/>'; //打印class_val

Class / * read introduced namespace, the interface is the same * /

echo \Index\Col\Ads\className::classv().'<br/>'; //打印class_val

/ * Introduce Constant * /

use const \Col\INSTANCE;

/ * Read a constant * /

. Echo INSTANCE '<br/>'; // Print const_val_col

/ * * Function is introduced /

use function \Col\functionName;

/ * * Function read /

echo functionName().'<br/>'; //打印function_val_col

/ * * Class or interface to the introduction /

use \Col\className;

/ * * Class or interface to read /

echo className::classv().'<br/>'; //打印classname_val_col

}

?>

The above example namespace Index's writing is fully qualified name, Index \ Col \ Ads If there is no previous \ global operator, it would become Index \ Index \ Col \ Ads namespace, be sure to pay attention .

Alias ​​Introduction example:

<?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{

/ * The introduction of Index \ Col \ Ads namespace, and set up an alias Ads * /

use Index\Col\Ads as Ads;

/ * The introduction of Index \ Col \ constant INSTANCE Ads namespace, and set alias con * /

use const Index\Col\Ads\INSTANCE as con;

/ * The introduction of Index \ Col \ function functionName Ads namespace, and set alias func * /

use function Index\Col\Ads\functionName as func;

/ * The introduction of Index \ class className Col \ Ads namespace, and set alias classn, alias interface and the way the same as * /

use Index\Col\Ads\className as classn;

. Echo Ads \ NS '<br/>'; // print namespace

. Echo con '<br/>'; // Print const_val

echo func () '<br/>';. // print function_val

echo classn::classv().'<br/>'; //打印class_val

}

?>

This example is a non-global namespace, so no global operator does not affect the imported namespaces.

Three: special supplement

1: namespace first character can not be numeric, must be a letter or an underscore, otherwise it will be reported farse error.

2: Constant define disposed in the default global namespace (exception: a file within the plurality of namespaces in parentheses wrapped default settings define the way the namespace is constant), so that if necessary constants of namespace , stated in constant need special name in, for example, define ( 'Index \ CON', 'CON') and define (__ nAMESPACE __. '\ CON', 'CON') both methods are set under constant CON namespace .

Constant Example:

<?php

namespace Col;

/ * Define default settings are global constants * /

define ( 'CON', 'global');

/ * Set up a special declaration under constant current namespace * /

define(__NAMESPACE__.'\CON','col');

/ * Set up a special statement under constant Index namespace * /

define('Index\CON','index');

/ * After the global operator directly with the constant name, so to get a global constant CON * /

var_dump (\ CON); // global

/ * No defined so obtained is CON constant current namespace * /

var_dump(CON); //col

/ * Global Limited, reads the corresponding constant CON Col namespace * /

var_dump(\Col\CON); //col

/ * Global Limited, reads the corresponding constant CON Index namespace * /

var_dump(\Index\CON); //index

?>

3: see example above, and to push function and class (interface) is not the same, functions and classes (interfaces) in the namespace set belong to the contents of the namespace, whether or not a file more namespace braces set.

Examples of functions and class: As can be seen in the functions and classes belonging namespace namespace

index.php

<?php

function foo(){

return 'global';

}

class fool{

static function ceshi(){

return 'global';

}

}

?>

col.php

<?php

namespace Col;

require './index.php'; // If you do not introduce index.php file, then 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 1

var_dump (\ Col \ foo ()); // prints 1

var_dump(\fool::ceshi()); //global

var_dump (fool :: ceshi ()); // prints 2

var_dump (\ Col \ fool :: ceshi ()); // prints 2

?>

4: Set the namespace of time, be careful not to use php keywords, such as type of function, class, abstract, otherwise will be reported parse error.

5: the same namespace, use between different documents not required to bring a namespace, use direct functions, constants, classes and interfaces on it.

6: a namespace classes, constants, interfaces, functions separately introducing another namespace, wherein the functions, constants, class, interface, if a collision occurs, without using the qualifier introduced separately precedence classes, constants, interfaces, functions .

Example:

indext.php

<?php

namespace Lic;

define(__NAMESPACE__.'\CON',1);

function func(){

echo 1;

}

class foo{

static function ceshi(){

return 1;

}

}

Only the introduction of namespaces

<?php

namespace Col;

require './indext.php';

use \Lic;

define (__ NAMESPACE __ '\ CON', 2.); // set constants must specify the namespace of the namespace, or is global constants

function func(){

echo 2;

}

class foo{

static function ceshi(){

return 2;

}

}

var_dump (CON); // Print 2

var_dump (namespace \ CON); // prints 2

func (); // Print 2

namespace \ func (); // prints 2

var_dump (foo :: ceshi ()); // prints 2

var_dump (namespace \ foo :: ceshi ()); // prints 2

If the case introduced separately classes, interfaces, functions, constants, and a name conflict is not defined, then with the introduced preferentially used:

<?php

namespace Col;

require './indext.php';

use \Lic\foo;

use function \Lic\func;

use const \Lic\CON;

define (__ NAMESPACE __ '\ CON', 2.); // set constants must specify the namespace of the namespace, or is global constants

function func(){

echo 2;

}

class foo{

static function ceshi(){

return 2;

}

}

var_dump (CON); // Print 1

var_dump (namespace \ CON); // prints 2

func (); // Print 1

namespace \ func (); // prints 2

var_dump (foo :: ceshi ()); // prints 1

var_dump (namespace \ foo :: ceshi ()); // prints 2

The issue of interpretation of PHP namespaces temporarily come to an end, I hope the content of these little friends help

Source: Pingxiang SEO

Guess you like

Origin www.cnblogs.com/1994jinnan/p/12239317.html