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!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
