1.命名空间-namespace
作用:区别不通文件中的相同类名。
例子:有两个文件,ClassA.php,ClassB.php。两个文件中都有一叫做work的类。将两个文件引入之后想要实例化ClassA.php中的work
1 //---------------ClassA.php---------------//
2 <?php
3 namespace ClassA;
4 class work {
5 public function __construct() {
6 echo 'this is class A';
7 }
8 }
9
10
11 //---------------ClassB.php---------------//
12 <?php
13 namespace ClassB;
14 class work {
15 public function __construct() {
16 echo 'this is class B';
17 }
18 }
19
20 //---------------Run.php---------------//
21 <?php
22 require 'ClassA.php';
23 require 'ClassB.php';
24
25 new \ClassA\work();
2.命名空间-use
作用:实例化的时候太长。
例子:多次实例化work的时候,每次都要new \ClassA\work();如果命名空间还比较长的话每次都要打很长一串。
1 <?php 2 require 'ClassA.php'; 3 require 'ClassB.php'; 4 5 use ClassA\work; 6 7 new work();
3.命名空间-use as
作用:别名,实例化两个同名的类
1 <?php 2 require 'ClassA.php'; 3 require 'ClassB.php'; 4 5 use ClassA\work as w1; 6 use ClassB\work as w2; 7 8 new w1(); 9 new w2();
4.抽象类与接口类
作用:接口类起到一个规范的作用;抽象类父类只是抽象一个功能,由子类具体实现。起到规范作用的同时还会执行。
1 <?php
2 abstract class fruit {
3 public function say() {
4 echo 'my color: ' . $this->getColor();
5 }
6
7 abstract function getColor();
8 }
9
10 class banana extends fruit {
11 public function getColor() {
12 return 'yellow';
13 }
14 }
15
16 class apale extends fruit {
17 public function getColor() {
18 return 'red';
19 }
20 }
21
22 $apale = new apale();
23 $apale->say();
5.性状
作用:两个完全不相关的类(不适合使用继承),有一部分重合功能。use本质上就相当于把性状的类粘贴过来。
1 <?php
2 trait repeat {
3 protected $v = 'v';
4 protected function test() {
5 echo 'this is test';
6 }
7 }
8
9 class classA {
10 use repeat;
11 public function __construct() {
12 echo $this->v;
13 $this->test();
14 }
15 }
16
17 $a = new ClassA();
6.生成器
作用:如例子1,foreach每迭代一次,for执行一次,返回一个值。对比例子2占用内存少很多。但是缺点是不能更改也不能存储。只能生成数据。
1 <?php
2
3 //例子1
4 function generator() {
5 for ($i=0; $i<10; $i++) {
6 echo '-';
7 yield $i;
8 }
9 }
10
11 foreach (generator() as $v) {
12 echo $v;
13 }
14
15 //例子2
16
17 function test() {
18 for ($i=0; $i<10; $i++) {
19 echo '-';
20 $res[] = $i;
21 }
22 return $res;
23 }
24
25 foreach (test() as $v) {
26 echo $v;
27 }
7.闭包
1 <?php
2
3 //-------使用-------
4
5 $add = 1;
6 $arr = array_map(function($v) use ($add) {
7 return $v + $add;
8 }, [1, 2, 3]);
9
10
11 //——-----判断是否是匿名函数——-----
12
13 function check($param) {
14 if ($param instanceof \Closure) {
15 return true;
16 }
17 }
18
19 //——-----bindTo——-----
20
21 class App {
22 protected $routes = array();
23 protected $responseBody;
24
25 public function addRoute($routePath, $routeCallBak) {
26 $this->routes[$routePath] = $routeCallBak->bindTo($this, __CLASS__);
27 }
28
29 public function dispatch($currentPath) {
30 foreach ($this->routes as $routhPath => $callBak) {
31 if ($routhPath == $currentPath) {
32 $callBak();
33 break;
34 }
35 }
36 echo $this->responseBody;
37 }
38 }
39
40 $app = new App();
41 $app->addRoute('user/add', function() {
42 $this->responseBody = json_encode(['name' => 'Alan']);
43 });
44
45 $app->dispatch('user/add');
来源:https://www.cnblogs.com/wangjianheng/p/12030058.html