Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Overview and definition of PHP namespaces

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "overview and definition of PHP namespaces". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the overview and definition of PHP namespaces".

Overview of namespaces

In PHP, namespaces are used to solve two types of problems encountered when writing class libraries or applications to create reusable code such as classes or functions:

Name conflicts between user-written code and classes / functions / constants or third-party classes / functions / constants within PHP.

Create an alias (or short) name for a long identifier name (usually defined to alleviate the first category of problems) to improve the readability of the source code.

The PHP namespace provides a way to combine related classes, functions, and constants. The following is an example of PHP namespace syntax:

Define namespaces

Although any legitimate PHP code can be included in a namespace, only three types of code are affected by the namespace: classes, functions, and constants. Namespaces are declared with the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, that is, allowing the contents of the same namespace to be stored in different files. Of course, you can also define multiple namespaces in the same file.

The copy code is as follows:

Namespace MyProject

Class MyClass

{

# code...

}

Define subnamespaces: much like directories and files, PHP namespaces also allow you to specify the names of hierarchical namespaces. Therefore, the name of the namespace can be defined in a hierarchical manner:

The copy code is as follows:

Namespace MyProject\ helper\ http

Class MyClass

{

# code...

}

Define multiple namespaces in the same file: there are two ways to declare multiple namespaces in the same file, but in actual programming practice, defining Dogo namespaces in the same file is highly discouraged. This approach is mainly used to merge multiple PHP scripts in the same file. The first method is listed below.

The copy code is as follows:

Namespace MyProject\ helper\ http

Class MyClass

{

# code...

}

Namespace MyProject\ helper\ request

Class MyClass

{

# code...

}

However, this method is strongly not recommended, and you can refer to the following braces definition:

The copy code is as follows:

Namespace MyProject\ helper\ http

{

Class MyClass

{

# code...

}

}

Namespace MyProject\ helper\ request

{

Class MyClass

{

# code...

}

}

Elements in the PHP namespace use the

Before discussing how to use namespaces, you must understand how PHP knows which elements in which namespace to use. Class names can be referenced in three ways:

An unqualified name, or a class name that does not contain a prefix, such as $a=new foo (); or foo::staticmethod ();. If the current namespace is currentnamespace,foo, it will be resolved to currentnamespace\ foo. If the code that uses foo is global and is not included in any namespace, foo is resolved to foo. Warning: if a function or constant in the namespace is not defined, the unqualified function or constant name is resolved to a global function name or constant name. For more information, see using Namespace: backing Global function name / constant name.

Qualified name, or a name that contains a prefix, such as $a = new subnamespace\ foo (); or subnamespace\ foo::staticmethod ();. If the current namespace is currentnamespace, foo is resolved to currentnamespace\ subnamespace\ foo. If the code that uses foo is global and is not included in any namespace, foo will be resolved to subnamespace\ foo.

A fully qualified name, or a name that contains a global prefix operator, for example, $a = new\ currentnamespace\ foo (); or\ currentnamespace\ foo::staticmethod ();. In this case, the foo is always resolved to the literal name (literal name) currentnamespace\ foo in the code.

Use Namespace: alias / Import

Allowing you to reference or import external fully qualified names through aliases is an important feature of namespaces. PHP namespaces support two ways of using aliases or imports: aliases for class names or aliases for namespace names. In PHP, aliases are implemented through the operator use.

Note that PHP does not support importing functions or constants.

The copy code is as follows:

Namespace foo

Use My\ Full\ Classname as Another

/ / the following example is the same as use My\ Full\ NSname as NSname

Use My\ Full\ NSname

/ / Import a global class

Use\ ArrayObject

Name resolution rule

Before we explain the name resolution rules, let's look at some important definitions:

Unqualified name Unqualified name: an identifier that does not contain a namespace delimiter in the name, such as Foo

Qualified name Qualified name: an identifier in a name that contains a namespace separator, such as Foo\ Bar

Fully qualified name Fully qualified name: an identifier in a name that contains a namespace delimiter and begins with a namespace delimiter, for example,\ Foo\ Bar. Namespace\ Foo is also a fully qualified name.

Name resolution follows the following rules:

Calls to fully qualified functions, classes, and constants are resolved at compile time. For example, new\ A\ B resolves to class A\ B.

All unqualified and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace A\ B\ C is imported to C, the call to C\ D\ e () is converted to A\ B\ C\ D\ e ().

Within the namespace, all qualified names that are not converted according to the import rules are preceded by the current namespace name. For example, if C\ D\ e () is called inside the namespace A\ B, C\ D\ e () will be converted to A\ B\ C\ D\ e ().

Unqualified class names are converted at compile time according to the current import rules (using the full name instead of the short import name). For example, if namespace A\ B\ C is imported to C, new C () is converted to new A\ B\ C ().

Within the namespace (for example, A\ B), function calls to unqualified names are resolved at run time. For example, the call to the function foo () is parsed like this:

1) find the function named A\ B\ foo () in the current namespace

2) try to find and call the function foo () in the global (global) space.

Calls to unqualified names or qualified name classes (non-fully qualified names) within the namespace (for example, A\ B) are resolved at run time. The following is the parsing process of calling new C () and new D\ E (): parsing new C ():

Look for class A\ B\ C in the current namespace.

Attempt to automate class A\ B\ C.

Resolution of new D\ E ():

Add the current namespace name before the class name to: a\ B\ D\ E, and then look for the class.

Try automounting class A\ B\ D\ E.

In order to reference a global class in the global namespace, you must use the fully qualified name new\ C ().

Example of Example name resolution

The copy code is as follows:

Thank you for your reading, the above is the content of "Overview and definition of PHP namespaces". After the study of this article, I believe you have a deeper understanding of the overview and definition of PHP namespaces, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report