摘要:中在基础集合类路由类中和分页类中等,都用到了对象遍历这个小知识点,这些类都是实现了这个接口,这个接口定义,返回的是迭代器对象。标准扩展库中提供了很多默认迭代器实现类,比较常用的是数组迭代器对象,参考官网迭代器。
说明:本文章主要讲述PHP的对象遍历(Iterator)知识点。由于Laravel框架中就在集合(Collection)中用到了对象遍历知识点,故记录并学习之。同时,作者会将开发过程中的一些截图和代码黏上去,提高阅读效率。
Laravel中在基础集合类IlluminateSupportCollection、路由类中IlluminateRoutingRouteCollection和分页类中IlluminatePaginationPaginator等,都用到了对象遍历这个小知识点,这些类都是实现了IteratorAggregate这个接口,这个接口定义getIterator(),返回的是迭代器对象。PHP标准扩展库中提供了很多默认迭代器实现类,比较常用的是数组迭代器对象ArrayIterator,参考官网:迭代器。
对象遍历(Iterator) 基本遍历PHP5提供了遍历对象属性的方法,而且默认是可见属性,如代码中foreach遍历对象属性,默认的都是可见属性:
$value) {
echo $key.":".$value.PHP_EOL;
}
输出的是:
name:PHP address:php.net
如果需要遍历对象的不可见属性,则在对象内部定义一个遍历方法:
public function unAccessIterator()
{
echo "Iterator the unaccess fields:".PHP_EOL;
foreach ($this as $key => $value) {
echo $key.":".$value.PHP_EOL;
}
}
对象外部访问:
$testIterator->unAccessIterator();
将可以遍历对象的不可见属性,输出结果:
Iterator the unaccess fields: name:PHP address:php.net sex:man age:20Iterator接口
PHP提供了Iterator接口,用来定义迭代器对象来自定义遍历,所以利用Iterator接口来构造迭代器,需要实现Iterator定义的几个方法:
composerPackage = $composerPackage;
}
public function unAccessIterator()
{
echo "Iterator the unaccess fields:".PHP_EOL;
foreach ($this as $key => $value) {
echo $key.":".$value.PHP_EOL;
}
}
/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current()
{
// TODO: Implement current() method.
echo "Return the current element:".PHP_EOL;
return current($this->composerPackage);
}
/**
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next()
{
// TODO: Implement next() method.
echo "Move forward to next element:".PHP_EOL;
return next($this->composerPackage);
}
/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key()
{
// TODO: Implement key() method.
echo "Return the key of the current element:".PHP_EOL;
return key($this->composerPackage);
}
/**
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid()
{
// TODO: Implement valid() method.
echo "Checks if current position is valid:".PHP_EOL;
return current($this->composerPackage) !== false;
}
/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind()
{
// TODO: Implement rewind() method.
echo "Rewind the Iterator to the first element:".PHP_EOL;
reset($this->composerPackage);
}
}
/*
$testIterator = new TestIterator();
foreach ($testIterator as $key => $value) {
echo $key.":".$value.PHP_EOL;
}
$testIterator->unAccessIterator();*/
$testIterator = new TestIterator([
"symfony/http-foundation",
"symfony/http-kernel",
"guzzle/guzzle",
"monolog/monolog"
]);
foreach ($testIterator as $key => $value) {
echo $key.":".$value.PHP_EOL;
}
成员变量$composerPackage是不可见的,通过实现Iterator接口,同样可以遍历自定义的可不见属性,输出结果如下:
Rewind the Iterator to the first element: Checks if current position is valid: Return the current element: Return the key of the current element: 0:symfony/http-foundation Move forward to next element: Checks if current position is valid: Return the current element: Return the key of the current element: 1:symfony/http-kernel Move forward to next element: Checks if current position is valid: Return the current element: Return the key of the current element: 2:guzzle/guzzle Move forward to next element: Checks if current position is valid: Return the current element: Return the key of the current element: 3:monolog/monolog Move forward to next element: Checks if current position is valid:IteratorAggregate接口
PHP真心为程序员考虑了很多,实现IteratorAggragate接口后只需实现getIterator()方法直接返回迭代器对象,就不需要实现Iterator接口需要的一些方法来创建一些迭代器对象,因为PHP已经提供了很多迭代器对象如ArrayIterator对象。所以再重构下上面代码:
class TestCollection implements IteratorAggregate{
...
/**
* @var array
*/
private $composerPackage;
...
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing Iterator or
* Traversable
* @since 5.0.0
*/
public function getIterator()
{
// TODO: Implement getIterator() method.
return new ArrayIterator($this->composerPackage);
}
}
$testCollection = new TestCollection([
"symfony/http-foundation",
"symfony/http-kernel",
"guzzle/guzzle",
"monolog/monolog"
]);
foreach ($testCollection as $key => $value) {
echo $key.":".$value.PHP_EOL;
}
同样的,能遍历$testCollection对象的不可见属性$composerPackage,输出结果:
0:symfony/http-foundation 1:symfony/http-kernel 2:guzzle/guzzle 3:monolog/monolog
文章开头聊到Laravel中就用到了IteratorAggragate这个接口,可以看看文件的源码。
总结:PHP提供的对象遍历特性功能还是很有用处的,下一篇准备看下generator生成器知识点,generator提供了另外一种方式定义Iterator。多多使用Laravel,研究Laravel源码并模仿之,也不错哦。
欢迎关注Laravel-China。
RightCapital招聘Laravel DevOps
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/21787.html
摘要:使用了来表示该,该接口也是对的抽象,暴露了一些常用方法判断是否满足要求的方法的读写相关操作获取元数据方法操作指针相关方法等等。本篇主要学习下相关使用。后续还会分享相关使用,到时见。 说明:本文主要学习guzzlehttp/guzzle package的使用,该package提供了一套发送HTTP请求API,就像phpunit package, mockery package, symf...
摘要:总结本文主要学习了启动时做的七步准备工作环境检测配置加载日志配置异常处理注册注册启动。 说明:Laravel在把Request通过管道Pipeline送入中间件Middleware和路由Router之前,还做了程序的启动Bootstrap工作,本文主要学习相关源码,看看Laravel启动程序做了哪些具体工作,并将个人的研究心得分享出来,希望对别人有所帮助。Laravel在入口index...
摘要:学习笔记之已经聊过使用了来设计,看源码发现其巧妙用了和的一些数组函数来设计。开发环境内置函数和看源码之前,先看下这几个内置函数的使用。学习笔记之实例化源码解析已经聊过的实例化,得到中的变量,即的实例化对象。后面再学习下的源码,到时见。 说明:本文主要学习Laravel的Middleware的源码设计思想,并将学习心得分享出来,希望对别人有所帮助。Laravel学习笔记之Decorato...
摘要:实际上,在中关闭主要包括两个过程保存当前到介质中在中存入。,学习下关闭的源码吧先。总之,关闭的第二件事就是给添加。通过对的源码分析可看出共分为三大步启动操作关闭。总结本小系列主要学习了的源码,学习了的三大步。 说明:在中篇中学习了session的CRUD增删改查操作,本篇主要学习关闭session的相关源码。实际上,在Laravel5.3中关闭session主要包括两个过程:保存当前U...
阅读 2142·2021-09-14 18:03
阅读 2549·2019-08-30 15:48
阅读 1361·2019-08-30 14:09
阅读 922·2019-08-30 12:55
阅读 3017·2019-08-29 11:29
阅读 1722·2019-08-26 13:43
阅读 2536·2019-08-26 13:30
阅读 2641·2019-08-26 12:17