Get the App
SLTechnology News&Howtos  ›  Development  › 

What does the namespace in php mean?

Shulou Source: shulou.com Published: 2022-06-03 15:44:37 09月19日 Update

This article mainly introduces "what is the meaning of namespaces in php". In daily operations, I believe that many people have doubts about the meaning of namespaces in php. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation, hoping to help you answer the doubts about "what is the meaning of namespaces in php?" Next, please follow the editor to study!

1. What is a namespace?

A namespace is a special scope that contains identifiers under that scope, and it is also an identifier itself. You can map the namespace to the directory of the operating system. A namespace is equivalent to a directory, classes, functions, constants in a namespace, and files in a directory. The file name in the same directory (namespace) cannot be the same, but there can be files with the same name in different directories.

2. What is the problem of using namespaces?

Resolve name conflicts, such as defining a class that happens to have the same name as a class within PHP or in a class library entered by include.

To improve the readability of the code, the namespace has an alias feature, which can help you give an alias to a class name that is more than ten characters long, thus shortening the code without having to worry about naming conflicts with other spaces.

3. Which code is affected by namespaces.

Three categories: class, function and constant. Only the three brothers are affected, what else to do, what to do. Speaking of constants, php 5.3 can use the const keyword to define constants, and before that, use define, and the namespace is valid only for the const keyword.

4. How to define namespaces

The copy code is as follows:

Namespace MyProject

Const CONNECT_OK = 1 after PHPP 5.3

Class Connection {/ *... * /}

Function connect () {/ *... /}

# example 2

Namespace MyProjectSubLevel

Const CONNECT_OK = 1 after PHPP 5.3

Class Connection {/ *... * /}

Function connect () {/ *... /}

Use `namespace space name` to declare a space. Before namespace, there cannot be any php statements other than declare statements, nor can there be any non-php code, not even spaces.

The following is the form of error:

The copy code is as follows:

$a = 1

Namespace MyProject

? > www.jb51.net

/ / Fatal error: Namespace declaration statement has to be the very first statement in the script...

In addition, the same namespace can be defined in multiple files, which is very useful for organizational frameworks. That is, files that start with the same namespace MyProject;, they are the same namespace. So be careful not to have the same class / function / constant name between files.

Of course, multiple namespaces can be defined in the same file, but this is highly discouraged. (understand that the same file defines multiple namespaces)

5. How to use namespaces

Namespaces can be used in three ways:

. Unqualified names-use class / function / constant names directly without using any delimiters, such as: new Foo (); foo (); echo FOO; when the file has a namespace

The copy code is as follows:

Tags: Space code file constant function method directory meaning function global dynamic name multiple parenthesis learning special same alias beginning form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Apple macOS Xiaomi