Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the php namespace use keyword

Shulou Source: shulou.com Published: 2022-06-03 08:02:18 11月15日 Update

In this issue, the editor will bring you about how to use the php namespace use keyword. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In php, the use keyword can be used to import namespaces with the syntax "use namespace;", or to import functions and constants and set aliases for them, with the syntax "use namespace as aliases;".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is a namespace?

In a broad sense, namespaces are a way to encapsulate things, and this abstract concept can be seen in many places. For example, in the operating system, directories are used to group related files, and for files in directories, it acts as a namespace.

Definition of namespaces

Namespaces (namespace) in PHP are added to PHP5.3, and if you know about C++, namespaces are nothing new. However, namespaces are quite important in PHP.

PHP namespaces can solve two types of problems:

Naming 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.

Define namespaces (using the keyword namespace)

Although any legitimate PHP code can be included in a namespace, only types of code such as classes (including abstract classes and traits), interfaces, functions, and constants are affected by the namespace.

The definition of the namespace needs to be declared by the keyword namespace, and the syntax format is as follows:

Namespace Namespace name

[example] Let's demonstrate how to define a namespace:

Except for the declare statement that defines how the source file is encoded, no non-PHP code (including whitespace) can appear before the namespace declaration.

Use Namespace: alias / Import

PHP allows the use of external namespaces through alias references or imports, which is an important feature of namespaces. This is a bit like creating symbolic links to other files or directories in a unix-like file system.

Use the use keyword to import namespaces, allowing functions and constants to be imported and aliased from PHP5.6. The syntax format is as follows:

Use namespace

In PHP, aliases are implemented through the operators use and as, and the syntax format is as follows:

Use Namespace as Alias

[example] use the use operator to import and use aliases.

Note: leading backslashes are unnecessary and not recommended for names in namespaces (fully qualified names that contain namespace delimiters, such as Foo\ Bar, and relatively global names that do not contain namespace delimiters, such as FooBar), because imported names must be fully qualified and will not be relatively resolved based on the current namespace.

To simplify the operation, PHP also supports importing multiple namespaces in one line, using them in the middle and separating them. The sample code is as follows:

Import operations are compiled, but dynamic class names, function names, or constant names are not.

In addition, the import operation affects only unqualified and qualified names. The fully qualified name is not affected by the import because it is determined.

This is how to use the php namespace use keyword shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Space name alias key keyword function constant code syntax file format directory example system influence important thing content delimiter operator Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL NVidia Shulou Tech Info Apple