After learning namespace
, here is a brief summary of namespaces.
1. Purpose of using namespace
In PHP
Function
, Class
, Constant
are not allowed to have the same name. In order to solve the problem of the same name among these three, namespaces appeared, so namespaces only affect classes, functions, and constants
(const
).
2. Namespace usage format
a. You can name a space
<?php namespace space1;//namespace关键字+空间名 代码; ?>
b.Also Multiple spaces can be named at the same time
<?php namespace space1;//namespace关键字 +空间名 代码1; namespace space2; 代码2; namespace space3; 代码3; .......//代码1,代码2,代码3,可相同亦可不同 ....... ?>
Note: If in a php file, the definition of the first space must be placed on the first line. However, everything has exceptions, and the only legal code before declaring a namespace is the declare
statement used to define the source file encoding. All non-PHP code, including whitespace, must not appear before a namespace declaration. For example, the following code will report an error.
<html> <?php namespace space1; namespace space2; ?> </html>
3. Namespace access
Namespace access is divided into: Unqualified space access
,Limited space access
, Fully qualified space access
.
a. Unlimited space access
<?php namespace space3; function f1(){ echo "space3"; } namespace space3\space2;//其中"\"代表space2是space3的子空间,同理space3是space2的父空间。 function f1(){ echo "space2"; } namespace space3\space2\space1; function f1(){ echo "space1"; } f1();//对上面空间成员进行访问,输出结果为:space1 ?>
b. Restricted space access
<?php namespace space2\space1; function f1(){ echo "space1"; } namespace space2; function f1(){ echo "space2"; } f1();//此时输出的是 space2 space1\f1();//此时输出的是space1 ?>
c .Fully qualified space access
<?php namespace space3; function f1(){ echo "space3"; } namespace space2; function f1(){ echo "space2"; } namespace space1; function f1(){ echo "space1"; } f1();//对上面空间成员进行访问,输出结果为:space1 \space3\f1();//对space3进行访问,输出结果为:space3 \space2\f1();//对space2进行访问,输出结果为:space2 ?>
4.Introducing space members
a.use
Space name\ Space name [as
alias]: Introduce the specified space into the current space. You can also use the as keyword to give an alias to the introduced space
b.use
Space name\space name\member class [as
Alias]: Introduce members in the specified space into the current space. Only classes can be introduced when introducing space members.
5. Some minor situations
##Once a namespace appears, access to space elements (classes, constants, functions) is limited to the space. If unqualified space access is used, the system will have the following parsing logic (qualified names or fully qualified names are Directly follow the path to find accurately)
First search in your own space
Secondly, if the element cannot be found, different space elements are processed differently
## System constants, system functions if found If not, the system class will not automatically go to the global space to find it
##<?php
namespace space3;
function f1(){
echo "space3";
}
//当前所有访问如果使用非限定名称都代表访问当前空间内的元素
f1();//访问space3下f1()函数
//想要访问函数
define('PI',3.14);//space3下没有define()函数,全局函数有
//想要访问系统常量
echo PHP_VERSION; //space3下没有define()函数,全局函数有
//想要访问类
//错误方案
//$m=new Mysqli('localhost','root','root');//系统会提示类不存在
//正确方案
$m= new \Mysqli('localhost','root','root');
?>
Recommended:
The above is the detailed content of Infinite matryoshka dolls, the naming path of namesapce. For more information, please follow other related articles on the PHP Chinese website!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
