Overview of the Use keyword in PHP, phpuse keyword Overview _php tutorial

Source: Internet
Author: User
Tags oscommerce

Overview of the Use keyword in PHP, phpuse keyword overview


Many open source systems, such as the oscommerce framework, will find the use of this keyword in their source code, such as the oscommerce framework in the index.php file appears in the source code:

Use Oscommerce\om\core\autoloader;use oscommerce\om\core\oscom;

In fact, PHP's use keyword was introduced from the php5.3 version above. Its role is to alias an external reference. This is an important feature of the namespace, which is similar to creating a connection flag for a file or directory on a UNIX-based file system.

The PHP namespace supports three aliases (or references):

1. Take an alias for a class

2. Take an alias for an interface

3. Take an alias for a namespace

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

<?phpnamespacefoo;usemy\full\classnameasanother;//thisisthesameasusemy\full\nsnameasnsnameusemy\full\nsname ;//importingaglobalclassusearrayobject; $obj =newnamespace\another;//instantiatesobjectofclassfoo\another$obj= Newanother;//instantiatesobjectofclassmy\full\classnamensname\subns\func ();//callsfunctionmy\full\nsname\subns \func$a=newarrayobject (Array (1));//instantiatesobjectofclassarrayobject//withoutthe "Usearrayobject" Wewouldinstantiateanobjectofclassfoo\arrayobject?>

Note that, for named names, the full name contains delimiters, such as Foo\bar, but not foobar, and "\foo\bar" the head of the "\" is not necessary, and it is not recommended to write. The introduction name must be a full name, and there is no program association with the current namespace .

PHP can also declare multiple on the same line, equivalent to the above wording

<?phpuseMy\Full\ClassnameasAnother,My\Full\NSname; $obj =newanother;//instantiatesobjectofclassmy\full\ Classnamensname\subns\func ();//callsfunctionmy\full\nsname\subns\func?>

It is also worth saying that the introduction is performed at compile time, so aliases do not affect dynamic classes , for example:

<?phpuseMy\Full\ClassnameasAnother,My\Full\NSname; $obj =newanother;//instantiatesobjectofclassmy\full\ classname$a = ' another '; $obj = New $a; Instantiates object of Class another?>

This is because the variable $ A is assigned a ' another ', and when compiled, a $ A is positioned to Classname.

For more detailed usage, readers can check the PHP manual or follow the related articles on this site.


PHP Use PHP namespace What's the matter?

1. Namespace Zend\http\phpenvironment;

This code defines a namespace that you can understand to define a domain name called zend\http\phpenvironment.

After the definition, the following class, interface, const, etc. are stated in the "domain" of the declaration. When referencing a containing file that declares a namespace, and wants to invoke something inside it, it must:

Adjust the current script also to this domain name, otherwise, you will have to use the full name of NAMESAPCE.

For example, inc.php file:

namespace Zend\http\phpenvironment;
Class Bar {}//defines a

When other files are called:

The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();

The second way to access Foo
namespace Foo; Adjust the current script to foo this NS domain, and namespace declaration must be in the first sentence
Require ' inc.php ';
$foo = new Bar ();

2. The Use keyword is used to alias the NS:

For example, the above

The first method of accessing Foo, with the full name
Require ' inc.php ';
$foo = new \zend\http\phpenvironment\bar ();

After using uses, the following wording is used:

Use \zend\http\phpenvironment as PE; Defining aliases

$foo = new \pe\bar (); Replace the original with a short alias.

If you omit the following as ..., then you can use the text in the last section instead, for example, above:

Use \zend\http\phpenvironment; Defining aliases
$foo = new \phpenvironment\bar (); Replace the original with a short alias.

================================================

Relevant content in the official PHP manual:

In PHP, the namespace namespace is used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:

1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.

The PHP namespace provides a way to group related classes, functions, and constants together.

The PHP namespace supports two ways of using aliases or imports: using aliases for class names, or aliases for namespace names, which are implemented using operator use. ... Remaining full text >>

What are the keywords in php?

The key word is the chess piece on the board, you can only use, cannot change and cannot go to Riga from one

You think I'm going to take pawn as a car and put four more horses with you, what are you doing?

So PHP doesn't do it!

I don't know if you understand?
Reference: www.gooddou.cn

http://www.bkjia.com/PHPjc/846189.html www.bkjia.com true http://www.bkjia.com/PHPjc/846189.html techarticle php in the Use keyword overview, phpuse keyword Overview Many open source systems such as the oscommerce framework, will find the use of this keyword in its source code, such as the oscommerce framework in index.php text ...

  • 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.