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.