-
Notifications
You must be signed in to change notification settings - Fork 698
/
Copy pathtest.php
45 lines (38 loc) · 1.11 KB
/
test.php
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
43
44
45
<?php
/**
* 行为型模式
*
* php迭代器模式
* 理解:遍历对象内部的属性,无需对外暴露内部的构成
* 下面我们来实现一个迭代器访问学校所有的老师
*
* @author TIGERB <https://github.com/TIGERB>
* @example 运行 php test.php
*/
// 注册自加载
spl_autoload_register('autoload');
function autoload($class)
{
require dirname($_SERVER['SCRIPT_FILENAME']) . '//..//' . str_replace('\\', '/', $class) . '.php';
}
/************************************* test *************************************/
use iterator\SchoolExperimental;
try {
// 初始化一个实验小学
$experimental = new SchoolExperimental();
// 添加老师
$experimental->addTeacher('Griffin');
$experimental->addTeacher('Curry');
$experimental->addTeacher('Mc');
$experimental->addTeacher('Kobe');
$experimental->addTeacher('Rose');
$experimental->addTeacher('Kd');
// 获取教师迭代器
$iterator = $experimental->getIterator();
// 打印所有老师
do {
$iterator->current();
} while ($iterator->hasNext());
} catch (\Exception $e) {
echo 'error:' . $e->getMessage();
}