PHP uses aliases and imports

Source: Internet
Author: User
Allowing the external fully qualified name to be referenced or imported through an alias is an important feature of the namespace. This is somewhat similar to the ability to create symbolic connections to other files or directories in a UNIX-like file system.

The PHP namespace supports two ways to use aliases or imports: Use aliases for class names, or use aliases for namespace names. Note that PHP does not support importing functions or constants.

In PHP, aliases are implemented using the operator use. Here is an example of using all the possible three 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 identical to use My\full\nsname as Nsname with use    my\full\nsname;    Import a global class use    \arrayobject;    $obj = new Namespace\another; Instantiate the Foo\another object    $obj = new Another;//Instantiate My\full\classname object    nsname\subns\func ();//Call Function my\full\nsname\ Subns\func    $a = new Arrayobject (Array (1));//Instantiate Arrayobject object    //Instantiate a foo\arrayobj if use \arrayobject is not used ECT Object?>

Note that a leading backslash is unnecessary and does not allow backslashes because the name in the namespace (the fully qualified name that contains the namespace delimiter, such as Foo\bar, and the relative global name that does not contain the namespace delimiter, such as FooBar), because the imported name must be fully qualified, Relative parsing is not based on the current namespace.

To simplify operations, PHP also supports using multiple use statements in a single row

Example #2 using the use operator to import/work with aliases, and one row contains multiple usage statements

<?php use    my\full\classname as another, my\full\nsname;    $obj = new Another; Instantiate My\full\classname object    nsname\subns\func ();//Call Function my\full\nsname\subns\func?>

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

Example #3 Import and dynamic names

<?php use    my\full\classname as another, my\full\nsname;    $obj = new Another; Instantiate a My\full\classname object    $a = ' another ';    $obj = new $a; A another object is actually?>

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

Example #4 Import and fully qualified names

<?php use    my\full\classname as another, my\full\nsname;    $obj = new Another; Instantiate the object of the My\full\classname class     $obj = new \another;//Instantiate the object of the another class     $obj = new another\thing;//Instantiate MY\FULL\CLASSN The object of the Ame\thing class     $obj = new \another\thing;//Instantiate an object of the Another\thing class?>
  • Related Article

    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.