PHP use keyword Overview

Source: Internet
Author: User
Tags oscommerce
This article mainly introduces the use keyword in PHP. For more information, see

This article mainly introduces the use keyword in PHP. For more information, see

Many open-source systems, such as the osCommerce framework, will find the keyword "use" in their source code. For example, this source code appears in the index. php file in the osCommerce framework:

Use osCommerce \ OM \ Core \ Autoloader; use osCommerce \ OM \ Core \ OSCOM;

In fact, the use keyword of php is introduced from php5.3 or a later version. It serves as an alias for an external reference. This is an important feature of a namespace. It is similar to creating a connection flag for a file or directory in a unix-based file system.

The PHP namespace supports three alias methods (or references ):

1. Alias for a class

2. Get an alias for an Interface

3. Get an alias for a namespace

All three methods are completed using the use keyword. The following are examples of the Three aliases:
// Example #1 importing/aliasing with the use operator

<? Phpnamespacefoo; useMy \ Full \ ClassnameasAnother; // thisisthesameasuseMy \ Full \ NSnameasNSnameuseMy \ Full \ NSname; // token; $ obj = newnamespace \ Another; // instantiatesobjectofclassfoo \ Another $ obj = newAnother; // instantiatesobjectofclassMy \ Full \ ClassnameNSname \ subns \ func (); // callsfunctionMy \ Full \ NSname \ subns \ func $ a = newArrayObject (array (1); // instantiatesobjectofclassArrayObject // witho Utthe "useArrayObject" wewouldinstantiateanobjectofclassfoo \ ArrayObject?>

Note that for a named name, the full name contains delimiters, such as Foo \ Bar, instead of FooBar ,, the "\" header of "\ Foo \ Bar" is unnecessary, and it is not recommended to write it like this. The import name must be the full name and has no program association with the current namespace.

PHP can declare multiple statements on the same line, which is equivalent to the above statement.

<? PhpuseMy \ Full \ ClassnameasAnother, My \ Full \ NSname; $ obj = newAnother; // instantiatesobjectofclassMy \ Full \ ClassnameNSname \ subns \ func (); // callsfunctionMy \ Full \ NSname \ subns \ func?>

It is also worth noting that the introduction is executed during compilation, so aliases do not affect dynamic classes, for example:

<? PhpuseMy \ Full \ ClassnameasAnother, My \ Full \ NSname; $ obj = newAnother; // instantiatesobjectofclassMy \ Full \ Classname $ a = 'another '; $ obj = New $; // instantiates object of class Another?>

Here, because the variable $ a is assigned a 'another ', $ a is located to the Classname during compilation.

For more detailed usage, refer to the php manual or follow the related articles on this site.

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.