Use keywords in PHP overview _php tips

Source: Internet
Author: User
Tags oscommerce

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

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

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

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

1. Alias a class

2. Alias for an interface

3. Alias a namespace

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

<?php
Namespacefoo;
Usemy\full\classnameasanother;

Thisisthesameasusemy\full\nsnameasnsname
Usemy\full\nsname;

Importingaglobalclass
Usearrayobject;

$obj =newnamespace\another;//instantiatesobjectofclassfoo\another
$obj =newanother;// Instantiatesobjectofclassmy\full\classname
Nsname\subns\func ();//callsfunctionmy\full\nsname\subns\func
$a =newarrayobject (Array (1));//instantiatesobjectofclassarrayobject
//withoutthe "Usearrayobject" Wewouldinstantiateanobjectofclassfoo\arrayobject
?>

Note that for the named name, the full name contains the separator, such as Foo\bar, and can not use Foobar, and "\foo\bar" the head of the "\" is not necessary, and do not recommend this. The introduction name must be a full name and has no program associated with the current namespace .

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

<?php
Usemy\full\classnameasanother,my\full\nsname;

$obj =newanother;//instantiatesobjectofclassmy\full\classname
nsname\subns\func ();//callsfunctionmy\full\ Nsname\subns\func
?>

It is also worth saying that the introduction is performed at compile time, so the alias does not affect the dynamic class , for example:

<?php
Usemy\full\classnameasanother,my\full\nsname;

$obj =newanother;//instantiatesobjectofclassmy\full\classname
$a = ' Another ';
$obj = New $a; Instantiates object of class Another
?>

Here because the variable $a assigned to the ' Another ', when compiling, will $a positioning to Classname.

More detailed usage the reader can consult the PHP manual or follow the relevant articles of 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.