PHP魔术常量以及命名空间

PHP魔术常量-trait

其他魔术常量都比较简单,就不具体展开了
主要说说trait
1.trait的功能和class的功能是很接近的,都可以定义一个类,并赋予方法
2.但是如果你要让这个类被中的方法被继承使用,那么就需要使用trait
举个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class cup
{
function water()
{
echo '我可以装水';
}
}
class wan
{
function food()
{
echo"我可以装食物";

}
}
class tools
{
//此时你如果想要这个tools同时拥有碗和杯子的功能就不可以了。
}
?>

但是当你把前两个class换成trait时,就可以执行这个功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
trait cup
{
function water()
{
echo '我可以装水';
}
}
trait wan
{
function food()
{
echo"我可以装食物";

}

}
class tools
{
use cup;
}
$makecup = new tools();
$makecup -> water();//-和>之间不可以有空格
?>

这边再理解一下trait嵌套trait:

1
2
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
<?php
trait 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

1
2
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
<?php
trait 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();
?>

如何避免同名方法?

1
2
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
<?php
trait 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 -> water();//-和>之间不可以有空格
//$makecup -> demo();
$makecup -> food();
?>

输出结果是我可以装一点点食物
或者可以更改名字来使用原本同名的方法

1
2
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
<?php
trait 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 -> water();//-和>之间不可以有空格
//$makecup -> demo();
$makecup -> food();
?>

主要就是这些了

接下来是命名空间 namespace

可以将命名空间理解为一个文件夹,所以不同文件夹就可以有相同类名。

然后第一个命名空间前不可以有任何代码
例:

1
2
3
4
5
6
7
8
<?php
$a=123;

namespace hello;
class test{

}
?>

对于命名空间,我们把其理解为一个文件夹,那么这个文件夹 何时终止呢,我运行代码所用的函数来自哪个文件夹呢?

1
2
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();//此时这个test依旧是属于下方的这个namespace
$a -> train; //这个输出的值为 这是一个小测试

那如果想要使用的namespace空间的类和方法则需要<?php

1
2
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这个test类所具有的属性方法
$b -> train();

还可以引入一个子空间的概念,如下面例子,意思就是world文件夹里面的TEST文件夹

1
2
3
4
5
6
7
8
9
namespace world\TEST\;//多加一个TEST,则引入了一个子文件夹的概念意味着,你后面可以命名world\TEST1
class test{
function train()
{
echo'这是一个小测试';
}
}
$b = new \world\TEST\test();//赋予b这个test类所具有的属性方法
$b -> train();

use和as的使用

直接举例子吧,比较容易理解

1
2
3
4
5
<?php
include '你的php文件名(假设是存放有namespace的文件)';//include可以导入文件
use world\TEST\test
$a= new test();
$a -> train();

这个时候使用的就是world空间里面的train,但是如果我同时用了两个use呢

1
2
3
4
5
6
7
<?php
include '你的php文件名(假设是存放有namespace的文件)';//include可以导入文件,后面要有单引号
use world\TEST\test
use hello\test;//这个时候有两个test,$a所要执行的test又无法分清了,所以要**改名字**
$a= new test();
$a -> train();
?>

改为如下

1
2
3
4
5
6
7
<?php
include '你的php文件名(假设是存放有namespace的文件)';//include可以导入文件,后面要有单引号
use world\TEST\test as test1
use hello\test as test2;// 这个时候已经用as更换名字了,那么接下来的运行就可以分清楚了
$a= new test1();
$a -> train();
?>

如果不想更改类名,那么就需要把方法所属的空间以及类都写下来以示区别。

Author

vague huang

Posted on

2020-10-18

Updated on

2020-10-20

Licensed under

Comments