This article will introduce to you how PHP declares a namespace, three ways to access space elements, and how to introduce a 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:
Effect:
Use
\namespace\to enter a namespace and call methods. Example:
Effect:<?php namespace China; // 定义命名空间 function getInfo () { echo '我是中国人'; } getInfo(); namespace USA; // 定义命名空间 function getInfo () { echo '我是美国人'; } getInfo(); \China\ getInfo(); ?>
1.3 Multi-level namespaceNaming The name of the space can be multi-level (sub-level namespace) For example:
Effect:
1.4 Three ways to access space elements1. Unqualified name access 2. Fully qualified name access 3. Restricted name access Example:
Effect:
2. Introduce the namespaceIntroduce namespace Fully qualified name access element Splicing rules for introducing namespaces
Example:
'; } namespace China\Beijing; // 定义命名空间 function getInfo () { echo 'China\Beijing
'; } use China\Beijing\Shunyi; getInfo(); Shunyi\getInfo(); ?>
Effect:
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; ?>
Effect:
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); ?>
Effect:
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: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 TutorialThe 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
Previous article:What is the state pattern in PHP? Learn it through examples
Next article:PHP mathematical function practice one: interesting analysis of max() and min() functions
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 Articles by Author
-
2023-04-26 17:59:18
-
2023-04-26 17:47:48
-
2023-04-26 17:41:42
-
2023-04-26 17:37:05
-
2023-04-26 17:31:25
-
2023-04-26 17:27:32
-
2023-04-25 19:57:58
-
2023-04-25 19:53:11
-
2023-04-25 19:49:11
-
2023-04-25 19:41:54
Latest Issues
PHP trim unicode spaces
I'm trying to trim unicode spaces such as this character and I was able to do it using thi...
From 2023-11-13 08:49:45
0
2
398
Related Topics
More>
-
About us
Disclaimer
Sitemap
-
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!