<?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; 开源</title>
	<atom:link href="http://www.54chen.com/tag/%e5%bc%80%e6%ba%90/feed" rel="self" type="application/rss+xml" />
	<link>http://www.54chen.com</link>
	<description>PHP、JAVA、缓存、架构、经验、分享</description>
	<lastBuildDate>Sat, 11 Feb 2012 17:09:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>解读PHP开源项目中列表和hook方法：while(has_items()): thme_ite();和apply_filters</title>
		<link>http://www.54chen.com/php-tech/php-open-source-projects-reading-lists-and-hook-methods-while-has_items-thme_ite-and-apply_filters.html</link>
		<comments>http://www.54chen.com/php-tech/php-open-source-projects-reading-lists-and-hook-methods-while-has_items-thme_ite-and-apply_filters.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:23:55 +0000</pubDate>
		<dc:creator>cc0cc</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[开源]]></category>

		<guid isPermaLink="false">http://www.54chen.com/php-tech/php-open-source-projects-reading-lists-and-hook-methods-while-has_items-thme_ite-and-apply_filters.html</guid>
		<description><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br>在wordpress,lilina等开源项目中，常常会看到一些莫名的代码，来无影去无踪，看上去很有意味，特地拿来分析分析，看看一般的作法。 part 1  列表 循环显示 先看代码： &#60;?php $num = 0; if(has_items()) { while(has_items()): the_item(); } ?&#62; 以上代码，出自某开源项目。。。 再进入the_item()函数内部，可以看到一个关键的函数： * Returns the current item * * @since 1.0 * * @return bool&#124;stdClass False if item doesn't exist, otherwise returns the specified &#8230;<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></description>
			<content:encoded><![CDATA[<a href="http://www.54chen.com"><img border="0" src="http://www.54chen.com/wp-content/themes/54chen2011/images/54chen-logo.gif"></a><br>以下内容由<a href="http://www.54chen.com">[五四陈科学院]</a>提供<br><p>在<a href="http://www.54chen.com/architecture/wordpress-nginx-php-cgi-mysql-memory-in-the-128m-to-install-and-optimize-the-vps-notes.html">wordpress</a>,lilina等开源项目中，常常会看到一些莫名的代码，来无影去无踪，看上去很有意味，特地拿来分析分析，看看一般的作法。</p>
<p><strong>part 1  列表 循环显示</strong></p>
<p>先看代码：</p>
<blockquote><p>&lt;?php<br />
$num = 0;<br />
if(has_items()) {<br />
while(has_items()): the_item();<br />
}<br />
?&gt;</p></blockquote>
<p>以上代码，出自某开源项目。。。</p>
<p>再进入the_item()函数内部，可以看到一个关键的函数：</p>
<blockquote><p>* Returns the current item<br />
*<br />
* @since 1.0<br />
*<br />
* @return bool|stdClass False if item doesn't exist, otherwise returns the specified item<br />
*/<br />
public function current_item() {<br />
$this-&gt;previous_item = $this-&gt;current_item;<br />
$this-&gt;current_item = '';</p>
<p>$item = each($this-&gt;items);<br />
$item = $item['value'];<br />
if(!$item)<br />
return false;</p>
<p>$this-&gt;current_item = $item;<br />
$this-&gt;current_feed = $item-&gt;feed;</p>
<p>return $item;<br />
}</p></blockquote>
<p>这个关键的函数是each()，来看手册：</p>
<h1>each</h1>
<div class="refnamediv"><a name="AEN12322"></a>(PHP 3, PHP 4, PHP 5)</p>
<p>each --       返回数组中当前的<a href="http://www.54chen.com/translate/dynamo-based-systems-designed-linkin-voldemort-voldemort-design-chinese-documents-i-am-a-chan-academy-of-sciences-translation-finalized.html">键／值</a>对并将数组指针向前移动一步</div>
<div class="refsect1"><a name="AEN12325"></a></p>
<h2>说明</h2>
<p>array <strong class="methodname">each</strong> ( array &amp;array )</p>
<p>返回 <var class="parameter">array</var> 数组中当前指针位置的键／值对并向前移动数组指针。键值对被返回为四个单元的数组，键名为      <var class="literal">0</var>，<var class="literal">1</var>，<var class="literal">key</var> 和 <var class="literal">value</var>。单元 <var class="literal">0</var> 和      <var class="literal">key</var> 包含有数组单元的键名，<var class="literal">1</var> 和      <var class="literal">value</var> 包含有数据。</p>
<p>如果内部指针越过了数组的末端，则 <strong class="function">each()</strong> 返回 <tt class="constant"><strong>FALSE</strong></tt>。</p>
<p>综合来看，其实就是一个做了一个游标一样的模型，利用each来操作一个数组或者相似的结构，每次在显示的时候，都有对数据数组的本身操作，而while里的has_item，只是作简单的判断有无而已。</p>
<p><strong>part2 hook</strong></p>
<p>第二种情况，是一个叫apply_filters的函数的<a href="http://www.54chen.com/architecture/wordpress-nginx-php-cgi-mysql-memory-in-the-128m-to-install-and-optimize-the-vps-notes.html">实现</a>。先看代码：</p>
<blockquote><p>function the_item() {<br />
global $lilina_items, $item;</p>
<p>$item = apply_filters('the_item', $lilina_items-&gt;current_item());<br />
}</p></blockquote>
<p>这是lilina里的一个函数，要看的是这个apply_filters方法，有一个简单的例子：</p>
<blockquote><p>function example_hook($string, $arg1, $arg2)<br />
{<br />
//Do stuff<br />
return $string;<br />
}<br />
$value = apply_filters('example_hook', 'filter me', 'arg1', 'arg2');</p></blockquote>
<p>apply_filters里面最关键的一个函数：</p>
<h1>call_user_func_array</h1>
<div class="refnamediv"><a name="AEN50629"></a>(PHP 4 &gt;= 4.0.4, PHP 5)</p>
<p>call_user_func_array --       Call a user function given with an array of parameters</p></div>
<div class="refsect1"><a name="AEN50632"></a></p>
<h2>Description</h2>
<p>mixed <strong class="methodname">call_user_func_array</strong> ( callback function, array param_arr )</p>
<p>Call a user defined function given by      <var class="parameter">function</var>, with      the <a href="http://www.54chen.com/php-tech/multi-nginx-configuration-of-single-php-fpm-approach-from-academy-of-sciences.html">parameters</a> in <var class="parameter">param_arr</var>.</p>
<p>其实这些渐渐被遗忘的<a href="http://www.54chen.com/webfe/academy-of-sciences-in-recent-years-a-good-article-summing-up.html">函数</a>，在高手们的使用下，对项目化的开发很有优势，随手记之。</div>
</div>
<br><br>想快点找到作者也可以到Twitter上留言: <a href="https://www.twitter.com/54chen" target="_blank">@54chen</a><br>或者你懒得带梯子上墙，请到新浪微博：<a href="http://t.sina.com.cn/54chen" target="_blank">@54chen</a>]]></content:encoded>
			<wfw:commentRss>http://www.54chen.com/php-tech/php-open-source-projects-reading-lists-and-hook-methods-while-has_items-thme_ite-and-apply_filters.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

