Using namespaces: Alias/import

Source: Internet
Author: User
Tags aliases constant

(PHP 5 >= 5.3.0, PHP 7)

It is an important feature of a namespace to allow fully qualified names that can be referenced or imported by an alias. This is somewhat akin to creating symbolic connections to other files or directories in a UNIX-like file system.

All PHP versions that support namespaces support three aliases or imports: Use aliases for class names, aliases for interfaces, or use aliases for namespace names. PHP 5.6 starts to allow import of functions or constants or to set aliases for them.

In PHP, aliases are implemented using the operator use. Here is an example of using all of the five possible ways to import:

Example #1 using the use operator to import/use aliases

<?php
namespace Foo;
Use My\full\classname as Another;

The following example is the same use My\full\nsname as the use my\full\nsname as Nsname
;

Import a global class use
arrayobject;

Importing a function (PHP 5.6+) use
function my\full\functionname;

Aliasing a function (PHP 5.6+) use
function My\full\functionname as func;

Importing a constant (PHP 5.6+) use
const my\full\constant;

$obj = new Namespace\another; Instantiate the Foo\another object
$obj = new Another;//Instantiate My\full\classname object
nsname\subns\func ();//Call Function My\full\nsnam E\subns\func
$a = new Arrayobject (Array (1));//Instantiate Arrayobject object
//If not using use \arrayobject, instantiate a Foo\array Object Objects
func ();//Calls function My\full\functionname
echo CONSTANT;//echoes the value of my\full\constant< C19/>?>

Note A leading backslash is unnecessary and not recommended for names in namespaces (fully qualified names that contain namespace delimiters, such as foo\bar, and relative global names such as FooBar) that do not contain namespace delimiters, because the name of the import must be fully qualified, Does not make relative resolution based on the current namespace.

To simplify operations, PHP also supports using multiple use statements on a single line

Example #2 to import/use aliases by using the usage operator, and a single line contains multiple uses statements

<?php use
my\full\classname as Another, My\full\nsname;

$obj = new Another; Instantiate the My\full\classname object
nsname\subns\func ();//Call function My\full\nsname\subns\func
?>

The import operation is performed on compilation, but the dynamic class name, function name, or constant name is not.

Example #3 Import and dynamic name <?php
Use me \ Full \ Classname as Another, my \ full \ Nsname;

$obj = new Another; Instantiate a My\full\classname object
$a = ' Another ';
$obj = new $a; Materialized a Another object
?>

In addition, the import operation affects only unqualified and qualified names. The fully qualified name is not affected by the import because it is OK.

Example #4 Import and fully qualified names

<?php use
my\full\classname as Another, My\full\nsname;

$obj = new Another; Instantiates object of class My\full\classname
$obj = new \another;//instantiates object of class Another
$ob j = new Another\thing; Instantiates object of class my\full\classname\thing
$obj = new \another\thing;//instantiates Object of class Ano Ther\thing
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.