Home > Backend Development > PHP Tutorial > Infinite matryoshka dolls, the naming path of namesapce

Infinite matryoshka dolls, the naming path of namesapce

autoload
Release: 2023-04-09 18:46:01
Original
3059 people have browsed it

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关键字+空间名 
代码;
 ?>
Copy after login

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,可相同亦可不同
.......
 ?>
Copy after login

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>
Copy after login

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
?>
Copy after login

b. Restricted space access

<?php
namespace space2\space1;
    function f1(){
     echo "space1";
      }
namespace space2;
function f1(){
     echo "space2";
      }
   f1();//此时输出的是 space2
   space1\f1();//此时输出的是space1
?>
Copy after login

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
?>
Copy after login

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;);

?>
Copy after login
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!

Related labels:
source:php.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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template