Use keywords in PHP

Source: Internet
Author: User
Tags autoloader oscommerce
Index when I read the osCommerce source code framework today. the keyword "use" is used in the PHP file. useosCommerceOMCoreAutoloader; useosCommerceOMCoreOSCOM; it is not very clear. I specially reviewed it. the keyword "use" is introduced by php5.3 or a later version to alias an external reference. This is a namespace

Index when I read the osCommerce source code framework today. the keyword "use" is used in the PHP file. use osCommerce \ OM \ Core \ Autoloader; use osCommerce \ OM \ Core \ OSCOM; it is not very clear, the use keyword is introduced by php5.3 and later versions. It serves as an alias for an external reference. This is a namespace

When I read the osCommerce source code framework today, the index. php file uses the useKeywords,

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

Not very clear. I made a special look, useKeywordsIntroduced in php5.3 or later versions.

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

Both useKeywords. The following are examples of the Three aliases:

// Example #1 importing/aliasing with the use operator

Namespace foo;
Use My \ Full \ Classname as Another;

// This is the same as use My \ Full \ NSname as NSname
Use My \ Full \ NSname;

// Importing a global class
Use ArrayObject;

$ Obj = new namespace \ Another; // instantiates object of class foo \ Another
$ Obj = new Another; // instantiates object of class My \ Full \ Classname
NSname \ subns \ func (); // callfunction My \ Full \ NSname \ subns \ func
$ A = new ArrayObject (array (1); // instantiates object of class ArrayObject
// Without the "use ArrayObject" we wocould instantiate an object of class foo \ 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.

Use My \ Full \ Classname as Another, My \ Full \ NSname;

$ Obj = new Another; // instantiates object of class My \ Full \ Classname
NSname \ subns \ func (); // callfunction My \ 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:

Use My \ Full \ Classname as Another, My \ Full \ NSname;

$ Obj = new Another; // instantiates object of class My \ Full \ Classname
$ A = 'another ';

$ Obj = New $ a; // instantiates object of class Another
?>

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


There are more detailed descriptions in this document. The length is limited. Let's talk about this...

Http://php.net/manual/en/language.namespaces.importing.php

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.