<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; IteratorAggregate</title>
	<atom:link href="https://www.phppan.com/tag/iteratoraggregate/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sun, 10 May 2026 02:26:45 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>YII框架中可以使用foreach遍历对象以及可以使用数组形式直接访问对象的原因</title>
		<link>https://www.phppan.com/2010/05/yii-foreach-arrayaccess/</link>
		<comments>https://www.phppan.com/2010/05/yii-foreach-arrayaccess/#comments</comments>
		<pubDate>Tue, 04 May 2010 01:05:27 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ArrayAccess]]></category>
		<category><![CDATA[Iterator]]></category>
		<category><![CDATA[IteratorAggregate]]></category>
		<category><![CDATA[Yii框架]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=672</guid>
		<description><![CDATA[YII框架中可以使用foreach遍历对象以及可以使用数组形式直接访问对象的原因 在YII框架的使用过程中，我 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>YII框架中可以使用foreach遍历对象以及可以使用数组形式直接访问对象的原因<br />
在YII框架的使用过程中，我们可以使用foreach直接遍历findAll等方法返回的对象的属性<br />
为什么呢？其实这与CModel实现的接口相关，接下来我们看下其实现的整个过程<br />
对于一个我们定义的model，它会继承虚类CActiveRecord，CActiveRecord类继承于CModel，如下所示：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> special <span style="color: #000000; font-weight: bold;">extends</span> CActiveRecord <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> CActiveRecord <span style="color: #000000; font-weight: bold;">extends</span> CModel<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
最关键的地方是CModel类实现了IteratorAggregate接口。<br />
而在CModel类中实现的getIterator方法返回的是这个model的所有属性，使用的迭代器是Yii框架实现的CMapIterator，而CMapIterator实现了Iterator接口</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* Returns an iterator for traversing the attributes in the model.
* This method is required by the interface IteratorAggregate.
* @return CMapIterator an iterator for traversing the items in the list.
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$attributes</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> CMapIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$attributes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这些就使得我们可以使用foreach直接遍历findAll等方法返回的对象<br />
关于此迭代器可以参看之前写的关于迭代器的文章：<br />
<a href="http://www.phppan.com/2010/04/php-source-24-iterator-false-value/">PHP源码阅读笔记二十四 ：iterator实现中当值为flase时无法完成迭代的原因分析</a></p>
<p>关于IteratorAggregate接口请移步<br />
<a href="http://cn.php.net/manual/en/class.iteratoraggregate.php">http://cn.php.net/manual/en/class.iteratoraggregate.php</a><br />
关于Iterator接口请移步<br />
<a href="http://cn.php.net/manual/en/class.iterator.php">http://cn.php.net/manual/en/class.iterator.php</a></p>
<p>
同样的道理，<br />
因为CModel实现了ArrayAccess接口，所以可以直接访问以数组的方式访问<br />
关于ArrayAccess接口请移步<br />
<a href="http://cn.php.net/manual/en/class.arrayaccess.php">http://cn.php.net/manual/en/class.arrayaccess.php</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/05/yii-foreach-arrayaccess/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
