PHP use keyword overview, PHPuse keyword Overview

Source: Internet
Author: User
Tags oscommerce

PHP use keyword overview, PHPuse keyword Overview

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;//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 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 $a; // 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.


What is php use php namespace?

1. namespace Zend \ Http \ PhpEnvironment;

This Code defines a namespace, which can be understood as a domain name named Zend \ Http \ PhpEnvironment.

After definition, the classes, interfaces, and const stated below are all in the declared "domain. When you reference a namespace inclusion file and want to call the content in it, you must:

Adjust the current script to this domain name. Otherwise, you must use the full name of namesapce.

For example, the inc. php file:

Namespace Zend \ Http \ PhpEnvironment;
Class Bar {} // defines a class

When other files are called:

// The first method to access Foo.
Require 'inc. php ';
$ Foo = new \ Zend \ Http \ PhpEnvironment \ Bar ();

// Method 2 for accessing Foo
Namespace Foo; // adjust the current script to the Foo ns domain, and the namespace statement must be in the first sentence
Require 'inc. php ';
$ Foo = new Bar ();

2. The use keyword aims to use the ns alias:

For example

// The first method to access Foo.
Require 'inc. php ';
$ Foo = new \ Zend \ Http \ PhpEnvironment \ Bar ();

After using uses, write as follows:

Use \ Zend \ Http \ PhpEnvironment as pe; // defines the alias

$ Foo = new \ pe \ Bar (); // use a short alias to replace the original

If the "as..." section is omitted, you can directly use the text in the last section instead. For example:

Use \ Zend \ Http \ PhpEnvironment; // defines the alias
$ Foo = new \ PhpEnvironment \ Bar (); // use a short alias to replace the original

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

Php official manual:

In PHP, The namespace is used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications:

1. the user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.
2. Create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem) to improve the readability of the source code.

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

The PHP namespace supports two alias or import methods: use an alias for the class name or namespace name, and use the alias through the use operator .... Remaining full text>

What are the keywords in php?

The keywords are the pawns on the board. You can only use them. You can neither change them nor add them to them.

What are you doing if I use my sister-in-a-car and put four more horses with you?

So PHP does not!

Do you understand?
Reference: www.gooddou.cn

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.