PHP基于SPL实现的迭代器步骤详解

php中世界最好的语言
Lepaskan: 2023-03-26 06:02:02
asal
1250 orang telah melayarinya

这次给大家带来PHP基于SPL实现的迭代器步骤详解,PHP基于SPL实现的迭代器注意事项有哪些,下面就是实战案例,一起来看一下。

现在有这么两个类,Department部门类、Employee员工类:

//部门类 class Department{ private $_name; private $_employees; function construct($name){ $this->_name = $name; $this->employees = array(); } function addEmployee(Employee $e){ $this->_employees[] = $e; echo "员工{$e->getName()}被分配到{$this->_name}中去"; } } //员工类 class Employee{ private $_name; function construct($name){ $this->_name = $name; } function getName(){ return $this->_name; } } //应用: $lsgo = new Department('LSGO实验室'); $e1 = new Employee("小锦"); $e2 = new Employee("小猪"); $lsgo->addEmployee($e1); $lsgo->addEmployee($e2);
Salin selepas log masuk

好了,现在LSGO实验室已经有两个部员了,现在我想把全部的部员都列出来,就是用循环来获取部门的每个员工的详情。

在这里我们用PHP中的SPL标准库提供的迭代器来实现。

《大话设计模式》中如是说:

迭代器模式:迭代器模式是遍历集合的成熟模式,迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构。

迭代器模式的作用简而言之:是使所有复杂数据结构的组件都可以使用循环来访问

假如我们的对象要实现迭代,我们使这个类实现 Iterator(SPL标准库提供),这是一个迭代器接口,为了实现该接口,我们必须实现以下方法:

current(),该函数返回当前数据项
key(),该函数返回当前数据项的键或者该项在列表中的位置
next(),该函数使数据项的键或者位置前移
rewind(),该函数重置键值或者位置
valid(),该函数返回 bool 值,表明当前键或者位置是否指向数据值

实现了 Iterator 接口和规定的方法后,PHP就能够知道该类类型的对象需要迭代。

我们使用这种方式重构 Department 类:

class Department implements Iterator { private $_name; private $_employees; private $_position;//标志当前数组指针位置 function construct($name) { $this->_name = $name; $this->employees = array(); $this->_position = 0; } function addEmployee(Employee $e) { $this->_employees[] = $e; echo "员工{$e->getName()}被分配到{$this->_name}中去"; } //实现 Iterator 接口要求实现的方法 function current() { return $this->_employees[$this->_position]; } function key() { return $this->_position; } function next() { $this->_position++; } function rewind() { $this->_position = 0; } function valid() { return isset($this->_employees[$this->_position]); } } //Employee 类同前 //应用: $lsgo = new Department('LSGO实验室'); $e1 = new Employee("小锦"); $e2 = new Employee("小猪"); $lsgo->addEmployee($e1); $lsgo->addEmployee($e2); echo "LSGO实验室部员情况:"; //这里其实遍历的$_employee foreach($lsgo as $val){ echo "部员{$val->getName()}"; }
Salin selepas log masuk

附加:

假如现在我们想要知道该部门有几个员工,如果是数组的话,一个count()函数就 ok 了,那么我们能不能像上面那样把对象当作数组来处理?SPL标准库中提供了 Countable 接口供我们使用:

class Department implements Iterator,Countable{ //前面同上 //实现Countable中要求实现的方法 function count(){ return count($this->_employees); } } //应用: echo "员工数量:"; echo count($lsgo);
Salin selepas log masuk

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

php7操作MongoDB增删改查步骤详解

PHP工厂方法设计模式案例详解

Atas ialah kandungan terperinci PHP基于SPL实现的迭代器步骤详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!