PHP魔术常量-trait
其他魔术常量都比较简单,就不具体展开了
主要说说trait
1.trait的功能和class的功能是很接近的,都可以定义一个类,并赋予方法
2.但是如果你要让这个类被中的方法被继承使用,那么就需要使用trait
举个例子                                                                                                         
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | <?phpclass cup
 {
 function water()
 {
 echo '我可以装水';
 }
 }
 class wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 }
 class tools
 {
 
 }
 ?>
 
 
 | 
但是当你把前两个class换成trait时,就可以执行这个功能
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | <?phptrait cup
 {
 function water()
 {
 echo '我可以装水';
 }
 }
 trait wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 
 }
 class tools
 {
 use cup;
 }
 $makecup = new tools();
 $makecup -> water();
 ?>
 
 | 
这边再理解一下trait嵌套trait:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | <?phptrait test
 {
 function demo()
 {
 echo"正在测试";
 }
 }
 trait cup
 {
 use test;
 function water()
 {
 echo '我可以装水';
 }
 }
 trait wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 
 }
 class tools
 {
 use cup;
 }
 $makecup = new tools();
 $makecup -> water();
 $makecup -> demo();
 ?>
 
 | 
如何同时使用多个trait
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 | <?phptrait test
 {
 function demo()
 {
 echo"正在测试";
 }
 }
 trait cup
 {
 use test;
 function water()
 {
 echo '我可以装水';
 }
 }
 trait wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 
 }
 class tools
 {
 use cup,wan;
 }
 $makecup = new tools();
 $makecup -> water();
 $makecup -> demo();
 $makecup -> food();
 ?>
 
 | 
如何避免同名方法?
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 
 | <?phptrait test
 {
 function demo()
 {
 echo"正在测试";
 }
 }
 trait cup
 {
 use test;
 function water()
 {
 echo '我可以装水';
 }
 function food()
 {
 echo"我可以装一点点食物";
 }
 }
 trait wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 
 }
 class tools
 {
 use cup,wan
 {
 cup::food insteadof wan;
 }
 }
 $makecup = new tools();
 
 
 $makecup -> food();
 ?>
 
 | 
输出结果是我可以装一点点食物
或者可以更改名字来使用原本同名的方法
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 
 | <?phptrait test
 {
 function demo()
 {
 echo"正在测试";
 }
 }
 trait cup
 {
 use test;
 function water()
 {
 echo '我可以装水';
 }
 function food()
 {
 echo"我可以装一点点食物";
 }
 }
 trait wan
 {
 function food()
 {
 echo"我可以装食物";
 
 }
 
 }
 class tools
 {
 use cup,wan
 {
 cup::food insteadof wan;
 wan::food as wafood;
 }
 }
 $makecup = new tools();
 
 
 $makecup -> food();
 ?>
 
 | 
主要就是这些了
接下来是命名空间 namespace
可以将命名空间理解为一个文件夹,所以不同文件夹就可以有相同类名。
然后第一个命名空间前不可以有任何代码
例:
| 12
 3
 4
 5
 6
 7
 8
 
 | <?php$a=123;
 
 namespace hello;
 class  test{
 
 }
 ?>
 
 | 
对于命名空间,我们把其理解为一个文件夹,那么这个文件夹 何时终止呢,我运行代码所用的函数来自哪个文件夹呢?
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | <?php
 namespace hello;
 
 class test{
 function  train(){
 echo'这是一个大测试';
 
 }
 
 }
 
 namespace world;
 class test{
 function train()
 {
 echo'这是一个小测试';
 }
 }
 
 $a = new test();
 $a -> train;
 
 | 
那如果想要使用的namespace空间的类和方法则需要<?php
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | namespace hello;class test{
 function  train(){
 echo'这是一个大测试';
 
 }
 
 }
 
 namespace world;
 class test{
 function train()
 {
 echo'这是一个小测试';
 }
 }
 $b = new \hello\test();
 $b -> train();
 
 | 
还可以引入一个子空间的概念,如下面例子,意思就是world文件夹里面的TEST文件夹
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | namespace world\TEST\;class test{
 function train()
 {
 echo'这是一个小测试';
 }
 }
 $b = new \world\TEST\test();
 $b -> train();
 
 | 
use和as的使用
直接举例子吧,比较容易理解
| 12
 3
 4
 5
 
 | <?phpinclude '你的php文件名(假设是存放有namespace的文件)';
 use world\TEST\test;
 $a= new test();
 $a -> train();
 
 | 
这个时候使用的就是world空间里面的train,但是如果我同时用了两个use呢
| 12
 3
 4
 5
 6
 7
 
 | <?phpinclude '你的php文件名(假设是存放有namespace的文件)';
 use world\TEST\test;
 use hello\test;//这个时候有两个test,$a所要执行的test又无法分清了,所以要**改名字**
 $a= new test();
 $a -> train();
 ?>
 
 | 
改为如下
| 12
 3
 4
 5
 6
 7
 
 | <?phpinclude '你的php文件名(假设是存放有namespace的文件)';
 use world\TEST\test as test1 ;
 use hello\test as test2;// 这个时候已经用as更换名字了,那么接下来的运行就可以分清楚了
 $a= new test1();
 $a -> train();
 ?>
 
 | 
如果不想更改类名,那么就需要把方法所属的空间以及类都写下来以示区别。