search
HomeBackend DevelopmentPHP TutorialInfinite matryoshka dolls, the naming path of namesapce

After learning namespace, here is a brief summary of namespaces.

1. Purpose of using namespace

In PHPFunction , 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(&#39;PI&#39;,3.14);//space3下没有define()函数,全局函数有
//想要访问系统常量
echo PHP_VERSION; //space3下没有define()函数,全局函数有


//想要访问类

//错误方案
//$m=new Mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);//系统会提示类不存在

//正确方案
$m= new \Mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;);

?>
Recommended:

php tutorial

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!

Statement
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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

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

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

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

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

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-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

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' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

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.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

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

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools