An in-depth analysis of how to declare and introduce namespaces in PHP

青灯夜游
Release: 2023-04-10 12:02:02
forward
3360 people have browsed it

This article will introduce to you how PHP declares a namespace, three ways to access space elements, and how to introduce a namespace.

An in-depth analysis of how to declare and introduce namespaces in PHP

1. Namespace

##1.1 Introduction

In a large project, you may encounter classes, functions, and constants with the same name. In order to distinguish these elements, we can store these elements in different namespaces. 1. Namespace is a package, used to store classes, functions, and constants in the project 2. Declare the namespace through the namespace keyword

##1.2 Declare the namespaceExample:

Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHPUse

\namespace\

to enter a namespace and call methods. Example:

<?php namespace China; // 定义命名空间 function getInfo () { echo &#39;我是中国人&#39;; } getInfo(); namespace USA; // 定义命名空间 function getInfo () { echo &#39;我是美国人&#39;; } getInfo(); \China\ getInfo(); ?>
Copy after login
Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

1.3 Multi-level namespaceNaming The name of the space can be multi-level (sub-level namespace) For example:

Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

1.4 Three ways to access space elements1. Unqualified name access 2. Fully qualified name access 3. Restricted name access Example:

Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2. Introduce the namespace

through

use

Introduce namespace Fully qualified name access element Splicing rules for introducing namespaces

Public space introduces space (remove the public part, the public part can only stay one level) space element

Example:
'; } namespace China\Beijing; // 定义命名空间 function getInfo () { echo 'China\Beijing
'; } use China\Beijing\Shunyi; getInfo(); Shunyi\getInfo(); ?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.1 Introducing space elementsIntroduction class: use Introduce functions: use function Introduce constants: use const Example:

'; // 引入函数 use function China\Beijing\Shunyi\getInfo; getInfo(); echo '
'; // 引入常量 use const China\Beijing\Shunyi\TYPE; echo TYPE; ?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.2 Give aliases to classes and functionsIf the introduced classes and functions have the same names as those in the current space, you need to give the introduced classes and functions aliases. Use alias as Example:

'; $stu1=new ChinaStudent; var_dump($stu1); ?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.3 Public spaceIf a page There is no namespace declaration space. The elements of this page are in the public space. Public spaces are represented by

\

Example:

'; } \getInfo(); ?>
Copy after login
Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.4 Namespace considerations1 , The namespace can only store classes, functions, and const constants. 2. There cannot be any code in front of the first namespace, including blank characters and header(). 3. Included files do not affect the current namespace.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of An in-depth analysis of how to declare and introduce namespaces in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!