Using namespaces: Aliases/imports

Source: Internet
Author: User

Using namespaces: Aliases/imports

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 the same as  use My\Full\NSname as NSname 
Use my\full\nsname;

//  Import a global class
Use \arrayobject;

$obj  = new namespace\Another; //  instantiations  foo\Another  objects
$obj  =  new Another; //  instantiating  my\full\classname Object
Nsname\subns\func (); //  calling function  my\full\nsname\subns\func
$a  = new arrayobject (Array (1)); //  instantiation   arrayobject  Object
//  if the   "Use \arrayobject"   is not used, a  foo\ArrayObject  object is instantiated
?

Note that leading backslashes are unnecessary and do not allow backslashes for names in namespaces that contain the fully qualified name of the namespace delimiter, such as Foo\bar and the relative global names that do not contain namespace delimiters, such as FooBar. Because the imported name must be fully qualified, it will not be resolved relative to the current namespace. Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\bar A s opposed to global names that does not, such as FooBar), the leading backslash are unnecessary and not allowed, as Import names must be fully qualified, and is not processed relative to the current namespace.

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

Reference php.net

http://php.net/manual/zh/language.namespaces.importing.php

Using namespaces: Aliases/imports

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.