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

What is the namespace of the PHP programming language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

This article mainly introduces "what is the namespace of PHP programming language". In daily operation, I believe many people have doubts about what is the namespace of PHP programming language. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "what is the namespace of PHP programming language"! Next, please follow the small series to learn together!

PHP namespaces were added in PHP 5.3, and if you've learned C#and Java, namespaces aren't new. However, there is still a very important meaning in PHP.

PHP namespaces solve two types of problems:

1. Name conflicts between user-written code and 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 alleviate the first type of problem) to improve readability of the source code.

。。。。。。。。。。。。。。。

define the namespace

By default, all constants, class, and function names are placed in global space, just as they were before PHP supported namespaces.

Namespaces are declared by the keyword namespace. If a file contains namespaces, it must declare namespaces before all other code. The syntax format is as follows;

You can also define different namespace codes in the same file, as follows:

To combine code in global non-namespaces with code in namespaces, use only the syntax in curly braces. Global code must be enclosed in an unnamed namespace statement with braces, as follows:

The only legal code before declaring namespaces is the declare statement that defines how source files are encoded. All non-PHP code, including whitespace, must not precede namespace declarations.

。。。。。。。。。。。。

sub namespace

Much like directories and files, PHP namespaces allow you to specify names for hierarchical namespaces. Therefore, namespace names can be defined hierarchically:

The above example creates the constant MyProject\Sub\Level\CONNECT_OK, the class MyProject\Sub\Level\Connection, and the function MyProject\Sub\Level\Connect.

。。。。。。。。。。。

namespace usage

Class names in PHP namespaces can be referenced in three ways:

http://www.iis7.com/b/wzjk/

1. An unqualified name, or a class name without a prefix, such as $a=new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, foo resolves to currentnamespace\foo. If code using foo is global and does not contain code in any namespace, foo resolves to foo. Warning: If a function or constant in a namespace is undefined, the unqualified function or constant name resolves to a global function or constant name.

2. A 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 resolves to currentnamespace\subnamespace\foo. If code using foo is global and does not contain code in any namespace, foo resolves to subnamespace\foo.

3. Fully qualified names, or names that include global prefix operators, for example,$a = new \currentnamespace\foo(); or\currentnamespace\foo::staticmethod();. In this case, foo is always resolved to the literal name in the code currentnamespace\foo.

Note: Access to any global class, function, or constant can be by fully qualified names

。。。。。。。。。。。。

Namespaces and Dynamic Language Features

PHP namespace implementations are influenced by the dynamic nature of the language itself.

Fully qualified names (class names that include namespace prefixes) must be used. Note that because there is no difference between qualified and fully qualified names in dynamic class, function, or constant names, leading backslashes are unnecessary.

。。。。。。。。。。。。

namespace keywords and__NAMESPACE__constants

PHP supports two abstract ways to access elements inside the current namespace, the__NAMESPACE__magic constant and the namespace keyword.

The value of the constant__NAMESPACE__is a string containing the name of the current namespace. Globally, excluding code in any namespace, it contains an empty string.

__NAMESPACE__example, code in namespace

The keyword namespace can be used to explicitly access elements in the current namespace or child namespaces. It is equivalent to the self operator in the class.

namespace operator, code in namespace

。。。。。。。。。。。。

Using namespaces: aliases/imports

PHP namespace support allows you to use aliases or imports in two ways: alias class names or alias namespace names.

In PHP, aliases are implemented using the use operator.

。。。。。。。。。。。

Using namespaces: fallback global functions/constants

When PHP encounters an unqualified class, function, or constant name in a namespace, it uses different precedence policies to resolve the name. Class names always resolve to names in the current namespace. Therefore, fully qualified names must be used when accessing class names that are internal to the system or not contained in namespaces

For functions and constants, if the function or constant does not exist in the current namespace, PHP will use the function or constant in the global space instead.

2. Global functions/constants backed up in namespace

。。。。。。。。。。。

global space

If no namespaces are defined, all classes and functions are defined in global space, just as they were before PHP introduced the concept of namespaces. Prefixing a name with\indicates that the name is in global space, even if it is in another namespace.

。。。。。。。。。。。

Order of namespaces

Since namespaces, the most error-prone thing is to use classes, and what kind of path the class looks for.

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

At this point, the study of "what is the namespace of PHP programming language" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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

Network Security

Wechat

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

12
Report