<?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; php</title>
	<atom:link href="http://www.ooso.net/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ooso.net</link>
	<description>Linux + Apache + Mysql + Php + Flash</description>
	<lastBuildDate>Mon, 15 Mar 2010 15:59:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>试着开源LiteCloud项目</title>
		<link>http://www.ooso.net/archives/554</link>
		<comments>http://www.ooso.net/archives/554#comments</comments>
		<pubDate>Mon, 15 Mar 2010 15:47:32 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[litecloud]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[tokyocabinet]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=554</guid>
		<description><![CDATA[所谓LiteCloud，无非就是前些天提到的LightCloud的php版本实现。这个和原来的python版本有一些区别，会造成不兼容，如下：


把Consistent Hashing算法换成了ketama，在pecl的memcached扩展里有简单方法可以实现，效率比单纯的php好很多，能快个10倍吧。没有重复造轮子，因此我很是得意。

				<span class="readmore"><a href="http://www.ooso.net/archives/554" title="试着开源LiteCloud项目">阅读全文（1866字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>所谓LiteCloud，无非就是<a href="http://www.ooso.net/archives/549">前些天提到的LightCloud</a>的php版本实现。这个和原来的python版本有一些区别，会造成不兼容，如下：</p>
<ul>
<ol>
<li>把Consistent Hashing算法换成了ketama，在pecl的memcached扩展里有简单方法可以实现，效率比单纯的php好很多，能快个10倍吧。没有重复造轮子，因此我很是得意。</li>
<li>静态方法调用时，不再支持原来的system参数，原版是用这个来支持多个LiteCloud集群。</li>
<li>第一个版本，利用memcached扩展来读取tokyo tyrant，所以目前仅支持简单的操作比如get, set, delete, increment。其实再努力一下，也可以支持更多的功能，比如redis。</li>
<li>去掉了原版的local_cache功能，我觉得这个功能完全可以放在外面，更灵活。</li>
</ol>
</ul>
<h2>项目主页</h2>
<p>目前托管在github上 —— <a href="http://github.com/volca/litecloud">LiteCloud</a> ，使用<a href="/tag/git">git</a>以及github的时间不太长，但是很喜欢，欢迎fork。</p>
<h2>使用示例</h2>
<p>用静态方法调用:</p>
<div class="igBar"><span id="lphp-3"><a href="#" onclick="javascript:showCodeTxt('php-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-3">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span> <span style="color:#FF0000;">'LiteCloud.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$config</span> = <span style="color:#000066;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'lookup1_A'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:41201'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'lookup1_B'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:51201'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'storage1_A'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:44201'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'storage1_B'</span> =&gt; <span style="color:#FF0000;">'127.0.0.,:54201'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">list</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$lookupNodes</span>, <span style="color:#0000FF;">$storageNodes</span><span style="color:#006600; font-weight:bold;">&#41;</span> = LiteCloud::<span style="color:#006600;">generateNodes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$config</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LiteCloud::<span style="color:#006600;">init</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$lookupNodes</span>, <span style="color:#0000FF;">$storageNodes</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LiteCloud::<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'hello'</span>, <span style="color:#FF0000;">'world'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">print</span> LiteCloud::<span style="color:#006600;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#008000; font-style:italic;"># =&gt; world</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">LiteCloud::<span style="color:#006600;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">print</span> LiteCloud::<span style="color:#006600;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#008000; font-style:italic;"># =&gt; nil </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>或者采用实例化的方式调用，这种方式能够支持多个LightCloud集群</p>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showCodeTxt('php-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span> <span style="color:#FF0000;">'LiteCloud.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$config</span> = <span style="color:#000066;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'lookup1_A'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:41201'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'lookup1_B'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:51201'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'storage1_A'</span> =&gt; <span style="color:#FF0000;">'127.0.0.1:44201'</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF0000;">'storage1_B'</span> =&gt; <span style="color:#FF0000;">'127.0.0.,:54201'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$cloud</span> = <span style="color:#000000; font-weight:bold;">new</span> LiteCloud<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$config</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$cloud</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'hello'</span>, <span style="color:#FF0000;">'world'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">print</span> <span style="color:#0000FF;">$cloud</span>-&gt;<span style="color:#006600;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#008000; font-style:italic;"># =&gt; world</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$cloud</span>-&gt;<span style="color:#006600;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">print</span> <span style="color:#0000FF;">$cloud</span>-&gt;<span style="color:#006600;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"hello"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#008000; font-style:italic;"># =&gt; nil </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>看上去和python版本差不多，对吧？</p>
<h2>性能测试</h2>
<p>这个部分是个重点，之前找到的lightcloud的php版本，性能比原版要慢一个数量级，我可不想在这个地方丢了份。</p>
<p>暂时做了个最简单的性能测试，测试脚本在test目录下。测试条件如下:</p>
<ul>
<ol>
<li>底层采用tokyo tyrant作为存储。</li>
<li>单个进程重复操作一万次，比如写一万次hello => world，这个测试条件和python版本完全一致</li>
<li>关闭了local_cache</li>
</ol>
</ul>
<h3>python版本的测试结果</h3>
<p><code><br />
Finished "Tyrant set" 10000 times in 2.50 sec [4002.0 operations pr.sec]<br />
Finished "Tyrant get" 10000 times in 1.04 sec [9583.7 operations pr.sec]<br />
Finished "Tyrant delete" 10000 times in 7.39 sec [1352.4 operations pr.sec]<br />
</code></p>
<h3>LiteCloud的测试结果</h3>
<p><code><br />
Using 1.8229308128357 time to set 10000 values. QPS:5485.67<br />
Using 0.71097207069397 time to get 10000 values. QPS:14065.25<br />
Using 2.1550960540771 time to delete 10000 values. QPS:4640.16<br />
</code></p>
<p>看上去还比较乐观，尤其是delete的性能翻了好几番，几乎要怀疑我在代码实现部分出了差错，在其它方面的表现也是全面超出，因为python版在hash_ring的实现上拖了后腿。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/554/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用nginx做为hiphop-php的前端服务器</title>
		<link>http://www.ooso.net/archives/545</link>
		<comments>http://www.ooso.net/archives/545#comments</comments>
		<pubDate>Sat, 27 Feb 2010 23:55:30 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=545</guid>
		<description><![CDATA[在邮件组里看到有人问能不能把多个hiphop-php编译后的程序跑在同一个端口上，想想也是合理的要求。如果一个服务器上跑了多个站点，那肯定都得用80端口，当大家共同租用服务器的时候，这个需求更为强烈。当时我所想到的解决办法是在前面搭个nginx之类的做代理，实际编译后的程序跑在别的端口，然后没过几天就看到了这份wiki - Using nginx as front server to HipHop。
简单的nginx配置示例
/etc/nginx/conf.d/ooso.conf:
PLAIN TEXT

				<span class="readmore"><a href="http://www.ooso.net/archives/545" title="使用nginx做为hiphop-php的前端服务器">阅读全文（839字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在邮件组里看到有人问能不能把多个hiphop-php编译后的程序跑在同一个端口上，想想也是合理的要求。如果一个服务器上跑了多个站点，那肯定都得用80端口，当大家共同租用服务器的时候，这个需求更为强烈。当时我所想到的解决办法是在前面搭个<a href="/?tag=nginx">nginx</a>之类的做代理，实际编译后的程序跑在别的端口，然后没过几天就看到了这份wiki - <a href="http://wiki.github.com/facebook/hiphop-php/using-nginx-as-front-server-to-hiphop">Using nginx as front server to HipHop</a>。</p>
<h2>简单的nginx配置示例</h2>
<p>/etc/nginx/conf.d/ooso.conf:</p>
<div class="igBar"><span id="lcode-7"><a href="#" onclick="javascript:showCodeTxt('code-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-7">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">server <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; listen *:<span style="color:#800000;color:#800000;">80</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; server_name *.<span style="">ooso</span>.<span style="">net</span> ooso.<span style="">net</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;location / <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root&nbsp; &nbsp;__SERVER_ROOT__;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index&nbsp; index.<span style="">html</span> index.<span style="">php</span> index.<span style="">htm</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;location ~ \.<span style="">php</span>$ <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header&nbsp; &nbsp;X-Real-IP&nbsp; &nbsp; &nbsp; &nbsp; $remote_addr;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header Host www.<span style="">ooso</span>.<span style="">net</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_pass&nbsp; &nbsp;http:<span style="color:#FF9933; font-style:italic;">//localhost:4247;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>把hiphop-php编译后的代码跑在4247端口，然后通过nginx把所有对php的请求转发到这个端口，看上去就像我们平常配置的php fastcgi，不是吗？</p>
<h2>这样做有什么好处</h2>
<ul>
<li>支持负载均衡</li>
<li>支持ssl</li>
<li>支持gzip压缩</li>
<li>用nginx来挡住DoS攻击</li>
<li>因为我们的代码需要经过编译才能上线，代码多起来这个时间还真不短，不能像之前单纯的php那样爽快覆盖就完事。把经过编译的最新代码部署在别的端口上，用nginx快速切换，应该是一个比较实际的用法。</li>
</ul>
<h2>额外的技巧</h2>
<p>看wiki学到的额外技巧。以下配置段可以防止某些人把别的垃圾域名指向你的主机，结果被搜索引擎认为你用多个域名搞了一堆重复的内容建设，降低搜索权重。</p>
<div class="igBar"><span id="lcode-8"><a href="#" onclick="javascript:showCodeTxt('code-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-8">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">server <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; listen *:<span style="color:#800000;color:#800000;">80</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; server_name _;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; location / <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; deny all;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/545/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>快速安装hiphop-php的捷径</title>
		<link>http://www.ooso.net/archives/543</link>
		<comments>http://www.ooso.net/archives/543#comments</comments>
		<pubDate>Sun, 21 Feb 2010 10:55:15 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[rpm]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=543</guid>
		<description><![CDATA[不得不说，现在安装hiphop-php实在是太麻烦了，如果有rpm包一次搞定那该多好？就说周边那些零散的依赖库，也有不少安装比较繁琐的硬骨头。
Centos用户的好消息
Update: 在centos 64位机上完全通过rpm安装hiphop-php的步骤也已经提供了。
在邮件组上看到有人提供了centos下安装hiphop相关的rpm列表，把这些rpm装好，再按照wiki上专心编译hiphop即可。也许再过一阵，就会有人直接提供hiphop的rpm包，那就彻底方便了。

				<span class="readmore"><a href="http://www.ooso.net/archives/543" title="快速安装hiphop-php的捷径">阅读全文（818字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>不得不说，现在安装hiphop-php实在是太麻烦了，如果有rpm包一次搞定那该多好？就说周边那些零散的依赖库，也有不少安装比较繁琐的硬骨头。</p>
<h2>Centos用户的好消息</h2>
<p><strong>Update</strong>: <a href="http://wiki.github.com/facebook/hiphop-php/installing-or-building-hiphop-php-via-rpm-on-centos-5">在centos 64位机上完全通过rpm安装hiphop-php的步骤</a>也已经提供了。</p>
<p>在邮件组上看到有人提供了<a href="http://github.com/johnwyles/hiphop-rpm-centos/downloads">centos下安装hiphop相关的rpm列表</a>，把这些<a href="/?tag=rpm">rpm</a>装好，再按照wiki上专心编译hiphop即可。也许再过一阵，就会有人直接提供hiphop的rpm包，那就彻底方便了。</p>
<p>rpm列表</p>
<ol>
<ul>
<li>libicu42-4.2.1-1.x86_64.rpm</li>
<li>libicu-devel-4.2.1-1.x86_64.rpm </li>
<li>icu-debuginfo-4.2.1-1.x86_64.rpm </li>
<li>icu-4.2.1-1.x86_64.rpm 		</li>
<li>libcurl4-devel-7.20.0-1.x86_64.rpm </li>
<li>libcurl4-7.20.0-1.x86_64.rpm 	</li>
<li>curl-debuginfo-7.20.0-1.x86_64.rpm</li>
<li>curl-7.20.0-1.x86_64.rpm 	</li>
<li>libevent-1.4.13-1.x86_64.rpm 	</li>
<li>boost-devel-1_37_0-1.x86_64.rpm </li>
<li>boost-debuginfo-1_37_0-1.x86_64.rpm</li>
<li>boost-1_37_0-1.x86_64.rpm 	</li>
<li>cmake-2.6.4-7.el5.x86_64.rpm</li>
</ul>
</ol>
<h2>Ubuntu 9.10用户的安装指南</h2>
<p><a href="http://wiki.github.com/facebook/hiphop-php/building-and-installing-on-ubuntu-910">Building and Installing on Ubuntu 9.10</a></p>
<p>看起来还是ubuntu用户最清爽。</p>
<p><strong>注：如果使用了32bit的ubuntu，请使用<a href="http://github.com/metagoto/hiphop-php">patch过的hiphop-php</a>进行编译。</strong></p>
<h2>Fedora用户的编译步骤</h2>
<p><a href="http://www.ioncannon.net/programming/918/building-hiphop-php-for-fedora-12-on-64-bit-and-32-bit-systems/">Building HipHop PHP for Fedora 12 on 64 bit and 32 bit Systems</a></p>
<p><del datetime="2010-02-24T13:02:52+00:00">BTW: 目前hiphop-php仅支持64位操作系统。</del></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/543/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>初次体验hiphop-php</title>
		<link>http://www.ooso.net/archives/541</link>
		<comments>http://www.ooso.net/archives/541#comments</comments>
		<pubDate>Sun, 21 Feb 2010 02:19:22 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=541</guid>
		<description><![CDATA[昨天facebook在github上发布了hiphop-php的源代码。之前听说这玩意能把php代码翻译成c++代码，然后带来巨大的性能提升，所以第一时间编译了一份hiphop-php。
我的机器环境是

Centos 5.3 x86_64

				<span class="readmore"><a href="http://www.ooso.net/archives/541" title="初次体验hiphop-php">阅读全文（1059字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>昨天facebook在<a href="http://github.com/facebook/hiphop-php">github</a>上发布了hiphop-php的源代码。之前听说这玩意能把php代码翻译成c++代码，然后带来巨大的性能提升，所以第一时间编译了一份hiphop-php。</p>
<h2>我的机器环境是</h2>
<ul>
<li>Centos 5.3 x86_64</li>
<li>8G内存</li>
<li>Intel(R) Xeon(R) CPU E5420  @ 2.50GHz</li>
</ul>
<h2>安装注意事项</h2>
<p>编译的时候碰到的问题很多，但是基本上都是按照<a href="http://wiki.github.com/facebook/hiphop-php/building-and-installing">wiki上的步骤</a>进行的。我觉得比较重要的几点：</p>
<ul>
<li>wiki上的Required Packages包包列表都要检查一遍，比如版本号，是否安装过，像binutils-dev这种就很容易忽略</li>
<li>版本符合的话，直接用yum安装这些包就可以了</li>
<li>wiki上有类似Boost 1.37 is the minimum version字样，说明开发者可能就是在这个版本下开发的，我试了下最新版本的boost，编译到后来反而出错</li>
<li>如果yum上没有符合版本的lib库，可以手动编译，但是编译时建议就放在自己的home下，比如：<br />
<code>./configure --prefix=/home/user</code></li>
<li>tbb Intel’s Thread Building Blocks这个包有些麻烦，记得按照wiki上说的步骤安装</li>
</ul>
<h2>测试hiphop-php</h2>
<p>安装完成之后，时间也不是太多，所以我仅仅是简单的测试了一个<a href="/?tag=php">php</a>文件，代码如下：</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showCodeTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$i</span> = <span style="color:#CC66CC;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$j</span> = <span style="color:#CC66CC;color:#800000;">0</span>; <span style="color:#0000FF;">$j</span> &lt;<span style="color:#CC66CC;color:#800000;">1000000</span>; <span style="color:#0000FF;">$j</span>++<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$i</span> += <span style="color:#0000FF;">$j</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">echo</span> <span style="color:#0000FF;">$i</span>, <span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>用hphp进行编译:</p>
<div class="igBar"><span id="lcode-15"><a href="#" onclick="javascript:showCodeTxt('code-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-15">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hphp/hphp test.<span style="">php</span> --keep-tempdir=<span style="color:#800000;color:#800000;">1</span> --log=<span style="color:#800000;color:#800000;">3</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>提示生成新的可执行文件</p>
<div class="igBar"><span id="lcode-16"><a href="#" onclick="javascript:showCodeTxt('code-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-16">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">/tmp/hphp_c9sbnG/program </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>做一下运行时间对比：</p>
<div class="igBar"><span id="lcode-17"><a href="#" onclick="javascript:showCodeTxt('code-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-17">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time php test.<span style="">php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.307s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.299s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.007s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time /tmp/hphp_c9sbnG/program</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.259s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.194s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.008s </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>没看出来编译成c++代码之后有太大的性能提升，估计是俺的使用手法问题？在邮件组里观察几天再说。</p>
<h2>Update</h2>
<p>facebook将优化之后的编译参数提交到了github，于是我重新编译并测试一遍这段相同的代码：</p>
<div class="igBar"><span id="lcode-18"><a href="#" onclick="javascript:showCodeTxt('code-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-18">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time /tmp/hphp_c9sbnG/program</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.140s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.076s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.006s </div>
</li>
</ol>
</div>
</div>
</div>
<p>
可以看到，经hiphop编译后的php，执行时间几乎快了一倍。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/541/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>快速创建pear/pecl的rpm</title>
		<link>http://www.ooso.net/archives/535</link>
		<comments>http://www.ooso.net/archives/535#comments</comments>
		<pubDate>Mon, 04 Jan 2010 04:32:41 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[rpm]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=535</guid>
		<description><![CDATA[目前使用的服务器为centos，使用yum以及rpm来维护系统好处多多：


安装卸载，升级rpm软件包只需一条命令即可

				<span class="readmore"><a href="http://www.ooso.net/archives/535" title="快速创建pear/pecl的rpm">阅读全文（535字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>目前使用的服务器为centos，使用yum以及rpm来维护系统好处多多：</p>
<ol>
<ul>
<li>安装卸载，升级rpm软件包只需一条命令即可</li>
<li>统一部署，便于维护</li>
<li>比手工编译的方式要快</li>
</ul>
</ol>
<p>于是我需要把日常使用到的一些php扩展做成rpm包，但是手工写spec文件比较繁琐，因此<a href="/?tag=pear">pear</a>的这个小工具<a href="http://pear.php.net/package/PEAR_Command_Packaging">PEAR_Command_Packaging</a>帮了不少忙。它会给pear工具新增一个选项：<br />
<code>pear make-rpm-spec [options]
<package-file></code></p>
<p>这个命令行将会创建一个rpm的.spec文件，包含指定pear包的定义，而且也适用于<a href="http://pecl.php.net">pecl</a>的php扩展。</p>
<h2>创建pear rpm包的例子</h2>
<p><code>$ cd /path/to/rpm-build-tree/SPECS<br />
$ pear make-rpm-spec ../SOURCES/Net_Socket-1.0.tgz<br />
Wrote RPM spec file PEAR::Net_Socket-1.0.spec<br />
$ rpm -bb PEAR::Net_Socket-1.0.spec<br />
...<br />
Wrote: /path/to/rpm-build-tree/RPMS/noarch/PEAR::Net_Socket-1.0-1.noarch.rpm<br />
</code></p>
<h2>关于make-rpm-spec的帮助</h2>
<p><code>pear help make-rpm-spec</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/535/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcache连接慢又一例</title>
		<link>http://www.ooso.net/archives/524</link>
		<comments>http://www.ooso.net/archives/524#comments</comments>
		<pubDate>Tue, 22 Dec 2009 13:56:53 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=524</guid>
		<description><![CDATA[继上次解决memcache连接慢问题以来，好长一段时间没在这个问题上翻过跟头。这一次我又在生产环境观察到php和memcache的连接时间经常会在50ms以上。
作为一个cache，占用了这么长的执行时间，天理何在？
实际的运行环境如下：


				<span class="readmore"><a href="http://www.ooso.net/archives/524" title="memcache连接慢又一例">阅读全文（699字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>继<a href="http://www.ooso.net/archives/479">上次解决memcache连接慢问题</a>以来，好长一段时间没在这个问题上翻过跟头。这一次我又在生产环境观察到php和memcache的连接时间经常会在50ms以上。</p>
<p>作为一个cache，占用了这么长的执行时间，天理何在？</p>
<p>实际的运行环境如下：</p>
<ol>
<ul>
<li>apache + mod_php</li>
<li><a href="http://pecl.php.net/memcache">php-memcache扩展</a>版本为2.2.5</li>
<li>memcache的并发连接数在400左右，相当少</li>
</ul>
</ol>
<p>这次memcache扩展用的是最新的稳定版，无可挑剔。所以刚开始我认为是网络环境的问题，于是直接采用telnet工具直连<a href="/?tag=memcache">memcache</a>进行测试，发现速度飞快！一点便秘感都没有！所以把目光仍然放回到memcache扩展上来，集中对比较慢的addServer方法各项参数进行排查。</p>
<h2>Memcache::addServer方法</h2>
<p><code>bool Memcache::addServer  ( string $host  [, int $port = 11211  [, bool $persistent  [, int $weight  [, int $timeout  [, int $retry_interval  [, bool $status  [, callback $failure_callback  [, int $timeoutms  ]]]]]]]] )</code></p>
<p>比对结果表明，$weight参数对memcache的连接时间有显著的影响，$weight的默认值为1，一旦设置为别的数值，连接时间便会由毫秒级变成50ms左右，立竿见影。</p>
<p>鉴于php-memcache扩展一贯恶劣的表现，俺不得不痛下决心迁移到新的<a href="http://pecl.php.net/memcached">memcached扩展</a>上。memcached扩展基于libmemcached开发，而且提供了丰富的接口方法，应该是更好的选择。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/524/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>php的echo为什么这么慢</title>
		<link>http://www.ooso.net/archives/517</link>
		<comments>http://www.ooso.net/archives/517#comments</comments>
		<pubDate>Tue, 15 Dec 2009 13:38:09 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=517</guid>
		<description><![CDATA[作为一个行走江湖多年的老中医，今天受命去解决一例前端页面展现缓慢的问题。问题页的情况如下：


apache + php

				<span class="readmore"><a href="http://www.ooso.net/archives/517" title="php的echo为什么这么慢">阅读全文（595字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>作为一个行走江湖多年的老中医，今天受命去解决一例前端页面展现缓慢的问题。问题页的情况如下：</p>
<ol>
<ul>
<li>apache + php</li>
<li>使用smarty模板输出内容</li>
<li>页面最终输出内容较大，80k+</li>
<li>页面执行时间在500ms以上</li>
</ul>
</ol>
<p>祭出法宝<a href="/archives/522">xhprof</a>对问题页面做了细致检查，发现页面的瓶颈竟然是模板（编译后的）中的一个echo语句，这个echo语句输出的字符串比较大，大概是50k+字节，花费时间为400多毫秒，占整个页面执行时间的80%。这样的echo输出在站点首页中其实是很常见的事情，没有数据库操作，按道理执行时间不应该这么长。</p>
<p>于是猛力使用搜索技能，最终在<a href="http://www.php.net/manual/en/function.echo.php#28843" href="_blank">php手册的echo部分</a>找到了一些蛛丝马迹，早在2003年就有前辈认为通过echo输出大字符串到客户端会引起服务器的性能问题，据我测试，在这个场景下使用print其实也是一样的慢。建议的解决办法是把字符串切割成更小的字符串输出，展现速度会有提升，输出函数如下：</p>
<div class="igBar"><span id="lcode-20"><a href="#" onclick="javascript:showCodeTxt('code-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-20">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function echobig<span style="color:#006600; font-weight:bold;">&#40;</span>$string, $bufferSize = <span style="color:#800000;color:#800000;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $splitString = str_split<span style="color:#006600; font-weight:bold;">&#40;</span>$string, $bufferSize<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; foreach<span style="color:#006600; font-weight:bold;">&#40;</span>$splitString as $chunk<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; echo $chunk;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>但是上面的处方不太对症，整个echobig的输出时间仍然在400毫秒左右，没有太大改善。</p>
<p>考虑到是输出大量内容到客户端比较慢，于是检查了apache的配置，原来还没打开deflate进行压缩，遂启用之。再次使用xhprof进行检查，这条echo的输出时间降低到5ms左右。</p>
<p>400ms到5ms，一个配置问题会产生80倍的差距，还真是省老钱了。这个故事告诉我们，<strong>压缩输出真的很重要。</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/517/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>在生产环境中使用php性能测试工具xhprof</title>
		<link>http://www.ooso.net/archives/522</link>
		<comments>http://www.ooso.net/archives/522#comments</comments>
		<pubDate>Wed, 05 Aug 2009 14:01:00 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=522</guid>
		<description><![CDATA[xhprof是facebook开源出来的一个php性能测试工具，也可以称之为profile工具，这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比，有很多类似之处。以前对xdebug有一些记录还可以供参考，但是它的缺点是对性能影响太大，即便是开启了profiler_enable_trigger参数，用在生产环境中也是惨不忍睹，cpu立刻就飙到high。
而xhprof就显得很轻量，是否记录profile可以由程序控制，因此，用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法：

				<span class="readmore"><a href="http://www.ooso.net/archives/522" title="在生产环境中使用php性能测试工具xhprof">阅读全文（815字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="http://mirror.facebook.net/facebook/xhprof/doc.html">xhprof</a>是facebook开源出来的一个php性能测试工具，也可以称之为profile工具，这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比，有很多类似之处。以前<a href="http://www.ooso.net/archives/359">对xdebug有一些记录还可以供参考</a>，但是它的缺点是对性能影响太大，即便是开启了profiler_enable_trigger参数，用在生产环境中也是惨不忍睹，cpu立刻就飙到high。</p>
<p>而xhprof就显得很轻量，是否记录profile可以由程序控制，因此，用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法：</p>
<p>以万分之一的几率启用xhprof，平时悄悄的不打枪。</p>
<div class="igBar"><span id="lphp-24"><a href="#" onclick="javascript:showCodeTxt('php-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-24">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">mt_rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span>, <span style="color:#CC66CC;color:#800000;">10000</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;xhprof_enable<span style="color:#006600; font-weight:bold;">&#40;</span>XHPROF_FLAGS_MEMORY<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#0000FF;">$xhprof_on</span> = <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在程序结尾处调用方法保存profile</p>
<div class="igBar"><span id="lphp-25"><a href="#" onclick="javascript:showCodeTxt('php-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-25">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xhprof_on</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#FF9933; font-style:italic;">// stop profiler</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#0000FF;">$xhprof_data</span> = xhprof_disable<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#FF9933; font-style:italic;">// save $xhprof_data somewhere (say a central DB)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>也可以用register_shutdown_function方法指定在程序结束时保存xhprof信息，这样就免去了结尾处判断，给个改写的不完整例子：</p>
<div class="igBar"><span id="lphp-26"><a href="#" onclick="javascript:showCodeTxt('php-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-26">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">mt_rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span>, <span style="color:#CC66CC;color:#800000;">10000</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;xhprof_enable<span style="color:#006600; font-weight:bold;">&#40;</span>XHPROF_FLAGS_MEMORY<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#000066;">register_shutdown_function</span><span style="color:#006600; font-weight:bold;">&#40;</span>create_funcion<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">''</span>, <span style="color:#FF0000;">"$xhprof_data = xhprof_disable(); save $xhprof_data;"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>至于日志，我暂时用的是最土的文件形式保存，定期清除即可。</p>
<p>BTW:xhprof生成的图形方式profile真是酷毙了，哪段代码成为瓶颈，一目了然。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/522/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>启用memcached压缩注意事项</title>
		<link>http://www.ooso.net/archives/475</link>
		<comments>http://www.ooso.net/archives/475#comments</comments>
		<pubDate>Wed, 13 May 2009 23:56:05 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=475</guid>
		<description><![CDATA[在php开发中，开启memcache的数据压缩存储是一件很简单的事情。在多数情况下，压缩数据不仅不会降低程序的执行效率，反倒会因为网络传输的开销降低，带来速度提升。看看最常用的Memcache::set方法:
bool Memcache::set  ( string $key  , mixed $var  [, int $flag  [, int $expire  ]] )
在这个方法中，将$flag设置为MEMCACHE_COMPRESSED即可启用memcache压缩存储。
这样做有什么弊端？

				<span class="readmore"><a href="http://www.ooso.net/archives/475" title="启用memcached压缩注意事项">阅读全文（1179字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在php开发中，开启<a href="/?tag=memcache">memcache</a>的数据压缩存储是一件很简单的事情。在多数情况下，压缩数据不仅不会降低程序的执行效率，反倒会因为网络传输的开销降低，带来速度提升。看看最常用的Memcache::set方法:<br />
<code>bool Memcache::set  ( string $key  , mixed $var  [, int $flag  [, int $expire  ]] )</code></p>
<p>在这个方法中，将$flag设置为MEMCACHE_COMPRESSED即可启用memcache压缩存储。</p>
<h3>这样做有什么弊端？</h3>
<p>如果没有做额外判断，每一次写入memcache都会启用压缩，不管数据的大小。对应的，每次获得数据都需要做一次解压缩的操作，这是典型的一刀切手法。实际上在数据很小的情况下，不需要压缩，在这个基础上压缩省不了多少空间。</p>
<h3>更好的压缩策略？</h3>
<p>好了，我的想法是在数据超过一定大小（比如2k）的情况下，才开启压缩。这个好办，捋起袖子就干，在调用Memcache::set方法之前，首先判断一下数据的大小，一个strlen就搞定了，再简单不过了。</p>
<div class="igBar"><span id="lphp-30"><a href="#" onclick="javascript:showCodeTxt('php-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-30">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span> = <span style="color:#000000; font-weight:bold;">new</span> Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">connect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'localhost'</span>, <span style="color:#CC66CC;color:#800000;">11211</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$flag</span> = <span style="color:#000066;">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#CC66CC;color:#800000;">2048</span> ? MEMCACHE_COMPRESSED : <span style="color:#CC66CC;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'mykey'</span>, <span style="color:#0000FF;">$data</span>, <span style="color:#0000FF;">$flag</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>有人可能会问了，array和object怎么办，这玩意可不能用strlen判断长度。</p>
<p>这还真能难住我一阵子，要知道把array/object写入memcache的时候，php会自动做serialize，再把它当作字符串插入memcache。</p>
<div class="igBar"><span id="lphp-31"><a href="#" onclick="javascript:showCodeTxt('php-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-31">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$flag</span> = <span style="color:#000066;">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">serialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#CC66CC;color:#800000;">2048</span> ? MEMCACHE_COMPRESSED : <span style="color:#CC66CC;color:#800000;">0</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>谁会采用这段代码？看起来非常山寨，而且serialize也不快，赔本买卖。</p>
<h3>更好的办法！</h3>
<p>上面的文字都是废话，直接看这段就好。<a href="http://cn2.php.net/manual/en/function.memcache-setcompressthreshold.php">Memcache::setCompressThreshold方法</a>可以包办之前所有的逻辑。</p>
<blockquote><p>Memcache::setCompressThreshold — Enable automatic compression of large values</p></blockquote>
<p><code>bool Memcache::setCompressThreshold  ( int $threshold  [, float $min_savings  ] )</code></p>
<p>举个例子，下面这段会自动启用压缩策略，当数据大于2k时，以0.2的压缩比进行zlib。</p>
<div class="igBar"><span id="lphp-32"><a href="#" onclick="javascript:showCodeTxt('php-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-32">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">setCompressThreshold</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2000</span>, <span style="color:#CC66CC;color:#800000;">0</span>.<span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>根据我的测试结果，<strong>setCompressThreshold方法会忽略Memcache::set的flag参数</strong>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/475/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>apache的RewriteMap使用心得</title>
		<link>http://www.ooso.net/archives/493</link>
		<comments>http://www.ooso.net/archives/493#comments</comments>
		<pubDate>Thu, 02 Apr 2009 13:57:47 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[apache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=493</guid>
		<description><![CDATA[在apache的环境下，rewrite还真是生活之友啊，时不时就得用上。前些日子有个需求，要将url重新转一转。
什么情况？
原来的url
http://www.xxx.com/demo/oldpage.php?param1=1&#038;param2=2

				<span class="readmore"><a href="http://www.ooso.net/archives/493" title="apache的RewriteMap使用心得">阅读全文（1250字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在apache的环境下，rewrite还真是生活之友啊，时不时就得用上。前些日子有个需求，要将url重新转一转。</p>
<h2>什么情况？</h2>
<p>原来的url</p>
<p>http://www.xxx.com<b>/demo/oldpage.php?param1=1&#038;param2=2</b></p>
<p>转换后的url</p>
<p>http://www.xxx.com/newpage.php?url=<b>%2Fdemo%2Fmypage.php%3Fparam1%3D1&#038;param2%3D2</b></p>
<p>需要把粗体部分的url进行urlencode，能看出上面的字符"?&#038;="都分别转义过，作为参数发给另外一个url。那么这时候请出rewrite还真是最合适不过了。</p>
<h2>坎坷的Rewrite经历</h2>
<p>查查rewrite手册，俺这才知道，转义这活，非得派出<a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#mapfunc">RewriteMap的map function</a>才能做的比较漂亮。现在只有四个内部map function可供差遣： </p>
<ul>
<li>toupper: Converts the key to all upper case.</li>
<li>tolower: Converts the key to all lower case.</li>
<li>escape: Translates special characters in the key to hex-encodings.</li>
<li>unescape: Translates hex-encodings in the key back to special characters.</li>
</ul>
<p>那么很快就有了第一个rewrite出现：<br />
<code><br />
RewriteMap escape int:escape<br />
RewriteRule ^/([^/]*)$ /newpage.php?mi_url_suffix=${escape:$1?%{QUERY_STRING}} [L,PT]<br />
</code></p>
<p>注：这里的int不是intger的意思，它是internal的缩写，表示调用内部函数。</p>
<p>看上去非常简单，跑起来貌似也正....常？且慢，俺打开RewriteLog一瞅，形式不容乐观啊，"&#038;"字符通通没有转义。看来是失败了，爬到狗狗上翻了一下，貌似escape对"?="之类的特殊字符是不做转义的，晕。</p>
<h2>RewriteMap到底</h2>
<p>接着细看apache的rewrite手册，发现RewriteMap还支持自定义脚本，那么还得使出俺的看家绝技——php了。首先弄一个能转义的php，必须非常简单，复杂了apache容易挂掉，写出来发现想复杂都挺难啊：</p>
<p>/usr/local/bin/escape.php</p>
<div class="igBar"><span id="lphp-34"><a href="#" onclick="javascript:showCodeTxt('php-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-34">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#!/usr/bin/php -f</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$in</span> = <span style="color:#000066;">trim</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">fgets</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDIN<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;">fputs</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDOUT, <span style="color:#000066;">urlencode</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$in</span><span style="color:#006600; font-weight:bold;">&#41;</span> . <span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\r</span><span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在这个脚本里可别使用php:://stdin之类的，具体原因查<a href="/category/php">php</a>手册。相应的，rewrite规则如下：<br />
<code><br />
RewriteMap escape prg:/usr/local/bin/escape.php<br />
RewriteRule ^/([^/]*)$ /newpage.php?mi_url_suffix=${escape:$1?%{QUERY_STRING}} [L,PT]<br />
</code></p>
<p>rewrite规则没有太大的改变，prg表示使用自定义脚本。现在这个版本总算正常运作了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/493/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>starling试用手记</title>
		<link>http://www.ooso.net/archives/506</link>
		<comments>http://www.ooso.net/archives/506#comments</comments>
		<pubDate>Sat, 21 Mar 2009 10:48:53 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[queue]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=506</guid>
		<description><![CDATA[twitter最近将ruby实现的消息队列服务器starling开源了，这是一个支持memcache协议的轻量级持久化服务器，因此使用php/perl/ruby/java等多种客户端都没问题，可以将较慢的处理逻辑通过消息队列放在后台处理，同时也支持多点分布式处理。周末找了个闲置的centos 5机器搭了一套starling试用，配合php的memcache扩展测试一番，有点意思。
starling安装步骤
centos默认不带ruby，得重新装，以下安装步骤都是以root身份跑的。


				<span class="readmore"><a href="http://www.ooso.net/archives/506" title="starling试用手记">阅读全文（745字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>twitter最近将ruby实现的<a href="http://github.com/starling/starling/tree/master">消息队列服务器starling</a>开源了，这是一个支持<a href="/?tag=memcache">memcache</a>协议的轻量级持久化服务器，因此使用php/perl/ruby/java等多种客户端都没问题，可以将较慢的处理逻辑通过消息队列放在后台处理，同时也支持多点分布式处理。周末找了个闲置的centos 5机器搭了一套starling试用，配合<a href="/?tag=php">php</a>的memcache扩展测试一番，有点意思。</p>
<h2>starling安装步骤</h2>
<p>centos默认不带ruby，得重新装，以下安装步骤都是以root身份跑的。<br />
<code><br />
yum install ruby ruby-devel rubygems<br />
gem install memcache-client starling<br />
</code></p>
<p>如果你使用yum找不到ruby的相关包，请记得<a href="http://www.ooso.net/archives/495">添加epel repository</a>。安装之后新增的程序：</p>
<ul>
<li>/usr/bin/starling   	#一个ruby脚本，starling的服务程序</li>
<li>/usr/bin/starling_top 	#starling的状态检查程序，可以查看starling的运行状态，类似memcache的状态显示，不同的是能够显示每个key的状态</li>
</ul>
<p>starling启动后默认会在22122端口蹲点守候。</p>
<p>为了使用方便，我修改了一个starling在centos下的启动脚本，放在/etc/init.d/starling，下载地址：<a href="http://customcode.googlecode.com/files/starling">http://customcode.googlecode.com/files/starling</a>。使用方法：<br />
<code>/etc/init.d/starling start|stop|restart</code></p>
<h2>测试程序</h2>
<p>以下是在测试中用的php脚本，说实话php在循环比较大的时候没啥优势，但是关键是简单，几行就搞定了。</p>
<p><strong>写入的测试程序</strong></p>
<div class="igBar"><span id="lcode-37"><a href="#" onclick="javascript:showCodeTxt('code-37'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-37">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#!/usr/bin/php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m = new Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m-&gt;<span style="">addServer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'127.0.0.1'</span>, <span style="color:#CC0000;">'22122'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$start = microtime<span style="color:#006600; font-weight:bold;">&#40;</span>true<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">for<span style="color:#006600; font-weight:bold;">&#40;</span>$i = <span style="color:#800000;color:#800000;">0</span>; $i &lt;<span style="color:#800000;color:#800000;">10000</span>; ++$i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">//echo $i, &quot;\n&quot;;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $m-&gt;<span style="">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'testtesttesttest'</span>, <span style="color:#CC0000;">'中文测试中文测试中文测试中文测试中文测试中文测试中中文测试中中文中a'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo <span style="color:#CC0000;">"time:"</span> . <span style="color:#006600; font-weight:bold;">&#40;</span>microtime<span style="color:#006600; font-weight:bold;">&#40;</span>true<span style="color:#006600; font-weight:bold;">&#41;</span> - $start<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#CC0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>读出来的测试程序</strong></p>
<div class="igBar"><span id="lcode-38"><a href="#" onclick="javascript:showCodeTxt('code-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-38">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#!/usr/bin/php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m = new Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m-&gt;<span style="">addServer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'127.0.0.1'</span>, <span style="color:#CC0000;">'22122'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">while<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; echo $m-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'test'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#CC0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; usleep<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// 休息一下，否则容易cpu 100%</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>性能测试</h2>
<p>测试条件:</p>
<ul>
<li>key的长度16B</li>
<li>value的长度100B,</li>
<li>8个并发写入进程</li>
<li>每个进程插入10,000条记录</li>
</ul>
<p>平均每个进程花了7秒完成写入操作，那么照这样计算：</p>
<p><strong>10000 * 8 / 7 = 每秒写入11428次</strong></p>
<p>以上测试进行的比较随意，而且我懒得插入大量数据来测试了，这个比较花时间，所以测试结果仅供参考。由于starling是目前twitter在生产环境中运行的，经过实践检验过，稳定性应该不成问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/506/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>131个字符的php framework</title>
		<link>http://www.ooso.net/archives/415</link>
		<comments>http://www.ooso.net/archives/415#comments</comments>
		<pubDate>Tue, 10 Mar 2009 06:40:03 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[framework]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=415</guid>
		<description><![CDATA[在friendfeed上看到这个链接 —— The 140 Characters Webapp Challenge!，这个比赛要求用140个字符的代码造就一个web应用。
里头有36个程序可供投票，基本上全是脚本语言大杂烩：php,perl,ruby,javascript。实现的应用也是五花八门，有相册，类twitter，小游戏，甚至还有php代码框架？摘录如下：
PLAIN TEXT
CODE:

				<span class="readmore"><a href="http://www.ooso.net/archives/415" title="131个字符的php framework">阅读全文（632字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在<a href="https://friendfeed.com">friendfeed</a>上看到这个链接 —— <a href="http://f055.net/article/final-wrap-up-of-the-140-characters-webapp-challenge/">The 140 Characters Webapp Challenge!</a>，这个比赛要求用140个字符的代码造就一个web应用。</p>
<p>里头有36个程序可供投票，基本上全是脚本语言大杂烩：php,perl,ruby,javascript。实现的应用也是五花八门，有相册，类twitter，小游戏，甚至还有php代码框架？摘录如下：</p>
<div class="igBar"><span id="lcode-41"><a href="#" onclick="javascript:showCodeTxt('code-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-41">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">require __DIR__.<span style="color:#CC0000;">'/c.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">if <span style="color:#006600; font-weight:bold;">&#40;</span>!is_callable<span style="color:#006600; font-weight:bold;">&#40;</span>$c = @$_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ?: function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> echo <span style="color:#CC0000;">'Woah!'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Error'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这段代码利用了php5.3的一些新特性：</p>
<ul>
<li>__DIR__</li>
<li>Anonymous functions</li>
<li>?:运算符</li>
</ul>
<p>代码只有131个字符，由于代码极为简陋，安全性也是没得保障的，只能算一个程序的统一入口罢了。</p>
<p>如果用php 5.2来写这段代码，大概就是：</p>
<div class="igBar"><span id="lcode-42"><a href="#" onclick="javascript:showCodeTxt('code-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-42">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">require dirname<span style="color:#006600; font-weight:bold;">&#40;</span>__FILE__<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0000;">'/c.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">if <span style="color:#006600; font-weight:bold;">&#40;</span>!is_callable<span style="color:#006600; font-weight:bold;">&#40;</span>$c = @$_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? $_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : create_function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">''</span>, <span style="color:#CC0000;">"echo 'Woah!';"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Error'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>如果要让这段代码变得实用点，可以在$c前面加上一个前缀，这样安全性会有进一步提升，代码也会相应的增加若干字节。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/415/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>解决memcache连接奇慢问题一例</title>
		<link>http://www.ooso.net/archives/479</link>
		<comments>http://www.ooso.net/archives/479#comments</comments>
		<pubDate>Sat, 07 Mar 2009 03:19:44 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=479</guid>
		<description><![CDATA[最近用xdebug观察线上程序的运行时间统计，发现往日里跑起来像飞的memcache居然是系统中拖后腿的耗时大户，连接时间特长。
运行环境

webserver是apache + php

				<span class="readmore"><a href="http://www.ooso.net/archives/479" title="解决memcache连接奇慢问题一例">阅读全文（788字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>最近用xdebug观察线上程序的运行时间统计，发现往日里跑起来像飞的<a href="/?tag=memcache">memcache</a>居然是系统中拖后腿的耗时大户，连接时间特长。</p>
<h2>运行环境</h2>
<ul>
<li>webserver是apache + php</li>
<li>php memcache extension版本是3.0.2，当时是最新的beta版...</li>
<li>有4个memcache server可供使用</li>
<li>代码中会利用php的Memcache::addServer依次连接四个memcache，长连接方式</li>
</ul>
<h2>现象</h2>
<p>完成四次addServer一共需要300ms以上，但是一旦连接上，获取单个item飞快，时间在3ms以下。<br />
更可恶的问题在于，虽然执行了四次Memcache::addServer，但是实际使用的始终是最后一个memcache，这实在让人崩溃。</p>
<h2>问题解决</h2>
<p>使用了一点搜索技巧，在pecl.php.net上找到了类似的bug: <a href="http://pecl.php.net/bugs/bug.php?id=13483">First get slow when using multiple memcached servers</a></p>
<p>这个bug的描述如下：</p>
<blockquote><p>
We are monitoring memcached performance and noticed that when we added a second memcached via Memcache::addServer the first get request is always slower than the subsequent ones although we are using persitent memcached connections. Switching from crc32 to fnv hashing didn't help either. Is that delay explainable
</p></blockquote>
<p>看起来是最新的memcache extension有一些问题，尝试将这个扩展降级成最新的稳定版2.2.6，然后重启apache看看，memcache连接过慢的问题果然已经解决。</p>
<h2>结论</h2>
<p>吃螃蟹果然是要付出代价的。。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/479/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>关于smarty3的一些说明</title>
		<link>http://www.ooso.net/archives/476</link>
		<comments>http://www.ooso.net/archives/476#comments</comments>
		<pubDate>Sun, 01 Feb 2009 10:10:16 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=476</guid>
		<description><![CDATA[这些天看到了smarty 3 alpha冒头了，于是花时间做了个走访调查。下面的文字基本上来自其readme：
基本文件文件结构

index.php

				<span class="readmore"><a href="http://www.ooso.net/archives/476" title="关于smarty3的一些说明">阅读全文（838字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>这些天看到了<a href="http://www.smarty.net">smarty 3 alpha</a>冒头了，于是花时间做了个走访调查。下面的文字基本上来自其readme：</p>
<h2>基本文件文件结构</h2>
<pre>
index.php
/libs/
  Smarty.class.php 		#主文件
/libs/sysplugins/		#内部plugin
  internal.*
/plugins/			#外部plugin,可自由扩充
  function.mailto.php
  modifier.escape.php
/templates/			#模板，可以是纯php或传统的smarty模板
  index.tpl
  index_view.php
</pre>
<h2>一个经典的smarty调用</h2>
<div class="igBar"><span id="lphp-47"><a href="#" onclick="javascript:showCodeTxt('php-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-47">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">require</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Smarty.class.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span> = <span style="color:#000000; font-weight:bold;">new</span> Smarty;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span>-&gt;<span style="color:#006600;">assign</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'foo'</span>,<span style="color:#FF0000;">'bar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span>-&gt;<span style="color:#006600;">display</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'index.tpl'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>和之前的版本似乎没什么差别</p>
<h2>SINGLETON</h2>
<p>这个有意义吗？</p>
<div class="igBar"><span id="lphp-48"><a href="#" onclick="javascript:showCodeTxt('php-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-48">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span> = Smarty::<span style="color:#006600;">instance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>模板</h2>
<p>之前的smarty模板，相当于重新定义了一套标签语言，那么smarty3提供了一种新的模板形式，直接支持<a href="/?tag=php">php</a>语法的模板。</p>
<p>但是问题就出来了，我们还有必要用模板吗？</p>
<p>引用php类型模板的一个例子：</p>
<div class="igBar"><span id="lphp-49"><a href="#" onclick="javascript:showCodeTxt('php-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-49">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span>-&gt;<span style="color:#006600;">display</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'php:mytemplate.tpl'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>模板中可以直接使用熟悉的语法： &lt;?=$foo?&gt; &lt;?=$bar?&gt;</p>
<p>使用php类型模板的话，安全问题需要自己解决。但这个对成熟的团队来说不是问题。</p>
<p>除此之外，新支持字符串类型的模板，感觉比较生猛，离模板之路也是渐行渐远：</p>
<div class="igBar"><span id="lphp-50"><a href="#" onclick="javascript:showCodeTxt('php-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-50">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$smarty</span>-&gt;<span style="color:#006600;">display</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'string:This is my template, {$foo}!'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>smarty3的相关链接</h2>
<ul>
<li><a href="http://smarty-php.googlecode.com/svn/branches/Smarty3Alpha/ ">Smarty 3 Alpha with SVN</a> -- 选择googlecode提供的svn服务还是蛮省钱的</li>
<li><a href="http://groups.google.com/group/smarty-developers">smarty 3开发者邮件组</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/476/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>在CentOS / Red Hat下安装PHP 5.x PECL Filter Extension</title>
		<link>http://www.ooso.net/archives/473</link>
		<comments>http://www.ooso.net/archives/473#comments</comments>
		<pubDate>Mon, 27 Oct 2008 08:22:30 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/473</guid>
		<description><![CDATA[php 5.2之后，默认会装有filter extension，这个扩展可以帮助php开发者过滤用户输入的内容，由php的创始人之一Rasmus Lerdorf提供，可以见我早期的文章介绍。
Q. How do I install filter extension for safely dealing with input parameters supplied via a web form using filter_var()?
A. This extension is part of PHP Core version 5.20 and above. Unfortunately, RHEL / CentOS comes with PHP version 5.1.6. So you need to install this extension by typing the following commands.

				<span class="readmore"><a href="http://www.ooso.net/archives/473" title="在CentOS / Red Hat下安装PHP 5.x PECL Filter Extension">阅读全文（1164字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>php 5.2之后，默认会装有filter extension，这个扩展可以帮助php开发者过滤用户输入的内容，由php的创始人之一Rasmus Lerdorf提供，可以见我<a href="/index.php/archives/202">早期的文章介绍</a>。</p>
<blockquote><p>Q. How do I install filter extension for safely dealing with input parameters supplied via a web form using filter_var()?<br />
A. This extension is part of PHP Core version 5.20 and above. Unfortunately, RHEL / CentOS comes with PHP version 5.1.6. So you need to install this extension by typing the following commands.</p></blockquote>
<p>由于RHEL / CentOS自带的php版本是5.1.6，那么只好手工安装filter扩展。安装过程：</p>
<h2>安装php-devel</h2>
<p>为了编译php extensions，需要安装php-devel包:<br />
<code># yum install php-devel</code></p>
<h2>下载php的源码</h2>
<p>php 5.1.6的源码树中没有包含php_pcre.h header文件, 所以需要另行下载一个php源码. 从php.net下载最新版本的源码并放到/opt目录:<br />
<code># cd /opt<br />
# elinks http://cn2.php.net/get/php-5.2.6.tar.bz2/from/cn.php.net/mirror</code></p>
<p>保存之后解压:<br />
<code># tar -jxvf php-5.2.6.tar.bz2</code></p>
<h2>下载filter extension</h2>
<p>在<a href="http://pecl.php.net">pecl</a>上找到最新的filter源码:<br />
<code># cd /opt<br />
# wget http://pecl.php.net/get/filter-0.11.0.tgz</code></p>
<h2>安装filter extension</h2>
<p>解压文件:<br />
<code># tar -jxvf filter-0.11.0.tgz<br />
# cd filter-0.11.0</code><br />
打开logical_filters.c文件:<br />
<code># vi logical_filters.c</code><br />
找到下面的代码行:</p>
<p><code>#include "ext/pcre/php_pcre.h"</code></p>
<p>修改成 (php_pcre.h的绝对路径):</p>
<p><code>#include "/opt/php-5.2.6/ext/pcre/php_pcre.h"</code></p>
<p>保存并关闭文件. 最后输入下面命令来编译filter扩展:<br />
<code># phpize<br />
# ./configure<br />
# make install</code></p>
<h2>配置Filter Extension</h2>
<p>输入下列命令:<br />
<code># echo 'extension=filter.so' > /etc/php.d/filter.ini </code></p>
<p>然后重启web server即可。</p>
<p>原文: <a href="http://www.cyberciti.biz/faq/rhel-cento-linux-install-php-pecl-filter/">CentOS / Red Hat Linux Install PHP 5.x PECL Filter Extension</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/473/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>php 5.3带来了什么(三)</title>
		<link>http://www.ooso.net/archives/472</link>
		<comments>http://www.ooso.net/archives/472#comments</comments>
		<pubDate>Wed, 24 Sep 2008 08:50:01 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/472</guid>
		<description><![CDATA[之前介绍的php 5.3的新特性，都是方便开发人员的东东。下面介绍个很讨虚拟主机提供商喜欢的特性。
增强的ini文件支持

CGI/ FastCGI支持类似.htaccess的INI配置

				<span class="readmore"><a href="http://www.ooso.net/archives/472" title="php 5.3带来了什么(三)">阅读全文（606字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>之前介绍的php 5.3的新特性，都是方便开发人员的东东。下面介绍个很讨虚拟主机提供商喜欢的特性。</p>
<h2>增强的ini文件支持</h2>
<ul>
<li>CGI/ FastCGI支持类似<b>.htaccess</b>的INI配置</li>
<li>每个目录下都可以有INI设置，ini的文件名取决于php.ini的配置，但是[PATH=/var/www/domain.com], [HOST=www.domain.com]段落的设置用户不能修改。</li>
<li>增强的error handling</li>
<li>允许在ini文件中定义变量和常量，可以在程序中直接调用。</li>
</ul>
<p>附上一段ini文件的例子</p>
<div class="igBar"><span id="lcode-52"><a href="#" onclick="javascript:showCodeTxt('code-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-52">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#用户自定义的php.<span style="">ini</span>文件名 <span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="">htaccess</span><span style="color:#006600; font-weight:bold;">&#41;</span>. 默认是<span style="color:#CC0000;">".user.ini"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user_ini.<span style="">filename</span> = <span style="color:#CC0000;">".user.ini"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#如果要禁用这个特性，设置为空值即可</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user_ini.<span style="">filename</span> =</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#用户自定义的php.<span style="">ini</span>文件TTL时长<span style="color:#006600; font-weight:bold;">&#40;</span>time-to-live<span style="color:#006600; font-weight:bold;">&#41;</span>，单位为秒，我理解为缓存过期时间。默认为<span style="color:#800000;color:#800000;">300</span>秒 </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user_ini.<span style="">cache_ttl</span> = <span style="color:#800000;color:#800000;">300</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>PATH=/var/www/domain.<span style="">com</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">variables_order = GPC</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">safe_mode = <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>my variables<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">somevar = “<span style="color:#800000;color:#800000;">1234</span>”</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">anothervar = $<span style="color:#006600; font-weight:bold;">&#123;</span>somevar<span style="color:#006600; font-weight:bold;">&#125;</span> ; anothervar == somevar</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>ini arrays<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color:#006600; font-weight:bold;">&#91;</span>bar<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#800000;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">123</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#800000;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#800000;color:#800000;">3</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/472/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>php 5.3带来了什么(二)</title>
		<link>http://www.ooso.net/archives/470</link>
		<comments>http://www.ooso.net/archives/470#comments</comments>
		<pubDate>Sun, 21 Sep 2008 06:44:53 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/470</guid>
		<description><![CDATA[接上文
性能提升
php 5.3的总体性能提升了5 - 15%


				<span class="readmore"><a href="http://www.ooso.net/archives/470" title="php 5.3带来了什么(二)">阅读全文（1275字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>接上文</p>
<h2>性能提升</h2>
<p>php 5.3的总体性能提升了5 - 15%</p>
<ul>
<li>md5()快了10-15%</li>
<li>Better stack implementation in the engine</li>
<li>Constants移到read-only内存里</li>
<li>exception处理过程改进(简化，opcodes更少)</li>
<li>(require/include)_once改进，去掉重复open</li>
<li>Smaller binary size &#038; startup size with gcc4</li>
</ul>
<h2>新语言特性</h2>
<h3>__DIR__</h3>
<p>在5.3以前，为了获得当前脚本的目录，需要一次函数调用</p>
<div class="igBar"><span id="lcode-60"><a href="#" onclick="javascript:showCodeTxt('code-60'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-60">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo dirname<span style="color:#006600; font-weight:bold;">&#40;</span>__FILE__<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// &lt; PHP 5.3 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在5.3，只需要一个魔术常量__DIR__就解决了。</p>
<div class="igBar"><span id="lcode-61"><a href="#" onclick="javascript:showCodeTxt('code-61'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-61">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo __DIR__; <span style="color:#FF9933; font-style:italic;">// &gt;= PHP 5.3 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>?:操作符</h3>
<p>便捷的<b>?:</b>操作符，可以从两个值/表达式中快速取得非空值。</p>
<div class="igBar"><span id="lcode-62"><a href="#" onclick="javascript:showCodeTxt('code-62'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-62">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = true ?: false; <span style="color:#FF9933; font-style:italic;">// true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = false ?: true; <span style="color:#FF9933; font-style:italic;">// true</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = <span style="color:#CC0000;">""</span> ?: <span style="color:#800000;color:#800000;">1</span>; <span style="color:#FF9933; font-style:italic;">// 1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = <span style="color:#800000;color:#800000;">0</span> ?: <span style="color:#800000;color:#800000;">2</span>; <span style="color:#FF9933; font-style:italic;">// 2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?: array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// array(1);</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = strlen<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">""</span><span style="color:#006600; font-weight:bold;">&#41;</span> ?: strlen<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"a"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// 1 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>__callStatic()</h3>
<p>新增了魔术方法__callStatic，功能和__call类似，但是仅对static方法有效。</p>
<div class="igBar"><span id="lcode-63"><a href="#" onclick="javascript:showCodeTxt('code-63'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-63">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class helper <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        static function __callStatic<span style="color:#006600; font-weight:bold;">&#40;</span>$name, $args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">                echo $name.<span style="color:#CC0000;">'('</span>.<span style="">implode</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">','</span>, $args<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0000;">')'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">helper::<span style="">test</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"foo"</span>,<span style="color:#CC0000;">"bar"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// test(foo,bar) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>动态调用static方法</h3>
<p>动态的调用静态方法？动静结合。</p>
<div class="igBar"><span id="lcode-64"><a href="#" onclick="javascript:showCodeTxt('code-64'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-64">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class helper <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        static function foo<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> echo __METHOD__; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = <span style="color:#CC0000;">"helper"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$b = <span style="color:#CC0000;">"foo"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a::$b<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// helper::foo </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>Late Static Binding</h3>
<p>不知道怎么译，可能留个原文更容易理解。静态方法的事件处理时机有变化，以前是在编译期处理，现在是执行期间处理。</p>
<p>在php 5.3之前，下面的代码会输出一个A，但是这不是咱们要的，whoami方法已经在class B中重新定义，它本该输出B才符合咱们想当然的思维。</p>
<div class="igBar"><span id="lcode-65"><a href="#" onclick="javascript:showCodeTxt('code-65'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-65">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function whoami<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   &nbsp; &nbsp;echo __CLASS__;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function identity<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     self::<span style="">whoami</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class B extends A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function whoami<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      echo __CLASS__;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">B::<span style="">identity</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// A &lt;-- PHP &lt;5.3 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>下面代码中使用了<b>static::whoami()</b>来调用静态方法。php 5.3之后，由于__CLASS__是在执行期被处理，那么这个例子中能顺利抓到class B。</p>
<div class="igBar"><span id="lcode-66"><a href="#" onclick="javascript:showCodeTxt('code-66'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-66">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function whoami<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      echo __CLASS__;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function identity<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      static::<span style="">whoami</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class B extends A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   public static function whoami<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      echo __CLASS__;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">B::<span style="">identity</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// B &lt;--&gt;= PHP 5.3 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>mysqlnd</h2>
<p>见<a href="http://www.ooso.net/index.php/archives/464">mysqlnd成为php 5.3中的默认mysql驱动</a></p>
<p>但是PDO_MySQL暂时还不支持mysqlnd，目前只有mysql(i)扩展可以用到</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/470/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>php 5.3带来了什么(一)</title>
		<link>http://www.ooso.net/archives/469</link>
		<comments>http://www.ooso.net/archives/469#comments</comments>
		<pubDate>Sun, 21 Sep 2008 00:49:31 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/469</guid>
		<description><![CDATA[之前陆续介绍了php 5.3的一些新特性，正好上周看到ZendCon关于5.3的ppt，介绍的更为详细，那么这里做个归总或者说翻译:)
Namespaces
php 5.3最大的改动，毫无疑问就是Namespaces(此前有一篇相关的PHP Namespaces FAQ)。这给php开发人员带来的好处不少，广为人们所诟病的函数命名问题也得到了解决。
代码更清晰

				<span class="readmore"><a href="http://www.ooso.net/archives/469" title="php 5.3带来了什么(一)">阅读全文（1229字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>之前陆续介绍了<a href="/index.php/archives/469">php 5.3的一些新特性</a>，正好上周看到ZendCon关于5.3的ppt，介绍的更为详细，那么这里做个归总或者说翻译:)</p>
<h2>Namespaces</h2>
<p>php 5.3最大的改动，毫无疑问就是Namespaces(此前有一篇相关的<a href="http://www.ooso.net/index.php/archives/418">PHP Namespaces FAQ</a>)。这给php开发人员带来的好处不少，广为人们所诟病的函数命名问题也得到了解决。</p>
<h3>代码更清晰</h3>
<p>5.3之前常见的代码，需要自定义前缀区分函数和类名</p>
<div class="igBar"><span id="lcode-74"><a href="#" onclick="javascript:showCodeTxt('code-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-74">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function MY_wrapper<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class MY_DB <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">define<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'MY_CONN_STR'</span>, <span style="color:#CC0000;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MY_wrapper<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">new MY_DB<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MY_CONN_STR; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>使用名称空间之后，代码看上去更加clean。</p>
<div class="igBar"><span id="lcode-75"><a href="#" onclick="javascript:showCodeTxt('code-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-75">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace MY;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function wrapper<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class DB <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">const CONN_STR = <span style="color:#CC0000;">''</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">use MY AS MY;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wrapper<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">new DB<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CONN_STR; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>一个文件中定义了多个namespace</h3>
<p>如果一个文件中定义了多个namespace，应该怎样处理？</p>
<div class="igBar"><span id="lcode-76"><a href="#" onclick="javascript:showCodeTxt('code-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-76">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace LIB;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class MySQL <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class SQLite <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$b = new SQLite<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace LIB_EXTRA;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class MScrypt <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$a = new MScrypt<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">var_dump<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        get_class<span style="color:#006600; font-weight:bold;">&#40;</span>$a<span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">        get_class<span style="color:#006600; font-weight:bold;">&#40;</span>$b<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>以上代码输出为：</p>
<div class="igBar"><span id="lcode-77"><a href="#" onclick="javascript:showCodeTxt('code-77'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-77">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">string<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">18</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#CC0000;">"LIB_EXTRA::MScrypt"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">string<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">11</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#CC0000;">"LIB::SQLite"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>php是解释执行的语言，以上结果合情合理。</p>
<h3>namespace的优先级</h3>
<p>namespace中定义的函数，类和常量优先，其次才是全局的。</p>
<div class="igBar"><span id="lcode-78"><a href="#" onclick="javascript:showCodeTxt('code-78'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-78">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace foo;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function strlen<span style="color:#006600; font-weight:bold;">&#40;</span>$foo<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> return htmlentities<span style="color:#006600; font-weight:bold;">&#40;</span>$foo<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo strlen<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// test </span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo ::<span style="">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// 4</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo namespace::<span style="">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"test"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// test </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>namespace和autoload的友情</h3>
<ul>
<li>autoload会根据namespace名称以及class名称来解析类文件位置</li>
<li>仅当namespace和全局范围都没找到class定义的情况下，autoload才会被触发 </li>
<li>在namespace中定义的__autoload不会被自动调用 </li>
</ul>
<div class="igBar"><span id="lcode-79"><a href="#" onclick="javascript:showCodeTxt('code-79'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-79">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function __autoload<span style="color:#006600; font-weight:bold;">&#40;</span>$var<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> var_dump<span style="color:#006600; font-weight:bold;">&#40;</span>$var<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#FF9933; font-style:italic;">// LIB::foo</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">require <span style="color:#CC0000;">"./ns.php"</span>; <span style="color:#008000;">/*</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">        &lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">                namespace LIB;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">                new foo();</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">*/</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>namespace一些辅料</h3>
<div class="igBar"><span id="lcode-80"><a href="#" onclick="javascript:showCodeTxt('code-80'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-80">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">namespace really::<span style="">long</span>::<span style="">pointlessly</span>::<span style="">verbose</span>::<span style="">ns</span>;  </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">__NAMESPACE__; <span style="color:#FF9933; font-style:italic;">// 新增的魔法常量，表示当前namespace名称</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class a<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>   </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">get_class<span style="color:#006600; font-weight:bold;">&#40;</span>new a<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// really::long::pointlessly::verbose::ns::a</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">use really::<span style="">long</span>::<span style="">pointlessly</span>::<span style="">verbose</span>::<span style="">ns</span>::<span style="">a</span> AS b; <span style="color:#FF9933; font-style:italic;">// 从名称空间中引用一个类 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>注：这里的内容节选自pdf <a href="http://ilia.ws/archives/187-Introduction-to-PHP-5.3-Slides.html">Introduction to PHP 5.3 Slides</a>，后文不再复述。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/469/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>mysqlnd成为php 5.3中的默认mysql驱动</title>
		<link>http://www.ooso.net/archives/464</link>
		<comments>http://www.ooso.net/archives/464#comments</comments>
		<pubDate>Wed, 06 Aug 2008 15:15:03 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=464</guid>
		<description><![CDATA[mysqlnd成为php 5.3中的默认mysql驱动，它有如下优点：

mysqlnd更容易编译: 因为它是php源码树的一个组成部分
mysqlnd和php内部机制结合更紧密，是优化过的mysql驱动

				<span class="readmore"><a href="http://www.ooso.net/archives/464" title="mysqlnd成为php 5.3中的默认mysql驱动">阅读全文（255字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>mysqlnd成为php 5.3中的默认mysql驱动，它有如下优点：</p>
<ul>
<li>mysqlnd更容易编译: 因为它是php源码树的一个组成部分</li>
<li>mysqlnd和php内部机制结合更紧密，是优化过的mysql驱动</li>
<li>mysqlnd更节省内存，从测试结果来看，比传统的mysql扩展节省40%的内存</li>
<li>mysqlnd更快</li>
<li>mysqlnd提供了丰富的性能统计功能</li>
<li>mysqlnd使用了PHP license以避免不必要的版权纠纷</li>
</ul>
<p>这个改动应同时对mysql和pdo_mysql扩展生效。</p>
<h3>mysqlnd是什么</h3>
<p><a href="http://blogs.mysql.com/kaj/2006/10/25/69/">mysqlnd</a>是<a href="/?tag=mysql">mysql</a>原装的php驱动</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/464/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>php 5.3将提供改进的Lambda函数</title>
		<link>http://www.ooso.net/archives/463</link>
		<comments>http://www.ooso.net/archives/463#comments</comments>
		<pubDate>Mon, 21 Jul 2008 01:32:24 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=463</guid>
		<description><![CDATA[从前的lambda函数
在php里面，传统方法是利用create_function来建立lambda函数，老实说，这个创建过程比较麻烦。拿php手册上的例子来说：
PLAIN TEXT
CODE:

				<span class="readmore"><a href="http://www.ooso.net/archives/463" title="php 5.3将提供改进的Lambda函数">阅读全文（86字）</a></span>]]></description>
			<content:encoded><![CDATA[<h3>从前的lambda函数</h3>
<p>在php里面，传统方法是利用create_function来建立lambda函数，老实说，这个创建过程比较麻烦。拿php手册上的例子来说：</p>
<div class="igBar"><span id="lcode-88"><a href="#" onclick="javascript:showCodeTxt('code-88'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-88">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$newfunc = create_function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'$a,$b'</span>, <span style="color:#CC0000;">'return &quot;ln($a) + ln($b) = &quot; . log($a * $b);'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo <span style="color:#CC0000;">"New anonymous function: $newfunc<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo $newfunc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">2</span>, M_E<span style="color:#006600; font-weight:bold;">&#41;</span> . <span style="color:#CC0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// outputs</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// New anonymous function: lambda_1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// ln(2) + ln(2.718281828459) = 1.6931471805599</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上头这样一段代码，恐怕一时半会还看不明白$newfunc到底是怎样的一个lambda函数。</p>
<h3>php 5.3的lambda函数语法</h3>
<p>改进的lambda基本语法</p>
<div class="igBar"><span id="lcode-89"><a href="#" onclick="javascript:showCodeTxt('code-89'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-89">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function &amp; <span style="color:#006600; font-weight:bold;">&#40;</span>parameters<span style="color:#006600; font-weight:bold;">&#41;</span> use <span style="color:#006600; font-weight:bold;">&#40;</span>lexical vars<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> body <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>创建一个简单lambda函数的例子。下面的例子看上去，语法和javascript甚至很接近了，对于我这样一个由javascript入门到php来的coder来说，这样的代码真是让人瞅着亲切。代码最后的<b>;</b>号是不可省略的，否则会碰到语法错误。</p>
<div class="igBar"><span id="lcode-90"><a href="#" onclick="javascript:showCodeTxt('code-90'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-90">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$lambda = function <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> echo <span style="color:#CC0000;">"Hello World!<span style="color:#000099; font-weight:bold;">\n</span>"</span>; <span style="color:#006600; font-weight:bold;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>调用方法很多</p>
<div class="igBar"><span id="lcode-91"><a href="#" onclick="javascript:showCodeTxt('code-91'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-91">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$lambda <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">call_user_func <span style="color:#006600; font-weight:bold;">&#40;</span>$lambda<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">call_user_func_array <span style="color:#006600; font-weight:bold;">&#40;</span>$lambda, array <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在函数中的简单调用</p>
<div class="igBar"><span id="lcode-92"><a href="#" onclick="javascript:showCodeTxt('code-92'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-92">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function replace_spaces <span style="color:#006600; font-weight:bold;">&#40;</span>$text<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $replacement = function <span style="color:#006600; font-weight:bold;">&#40;</span>$matches<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return str_replace <span style="color:#006600; font-weight:bold;">&#40;</span>$matches<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#CC0000;">' '</span>, <span style="color:#CC0000;">'&amp;nbsp;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0000;">' '</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return preg_replace_callback <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'/( +) /'</span>, $replacement, $text<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>甚至可以把lambda函数作为结果返回，这样引申的语法变化会相当丰富，想想javascript就知道了</p>
<div class="igBar"><span id="lcode-93"><a href="#" onclick="javascript:showCodeTxt('code-93'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-93">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function getAdder<span style="color:#006600; font-weight:bold;">&#40;</span>$x<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return function <span style="color:#006600; font-weight:bold;">&#40;</span>$y<span style="color:#006600; font-weight:bold;">&#41;</span> use <span style="color:#006600; font-weight:bold;">&#40;</span>$x<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// or: lexical $x;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; return $x + $y;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>还可以导入外部变量到lambda函数，通过关键字<strong>use</strong>完成。如果没有这个关键字，以前可能要通过<strong>global</strong>来传递这些变量。</p>
<div class="igBar"><span id="lcode-94"><a href="#" onclick="javascript:showCodeTxt('code-94'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-94">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function replace_in_array <span style="color:#006600; font-weight:bold;">&#40;</span>$search, $replacement, $array<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $map = function <span style="color:#006600; font-weight:bold;">&#40;</span>$text<span style="color:#006600; font-weight:bold;">&#41;</span> use <span style="color:#006600; font-weight:bold;">&#40;</span>$search, $replacement<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; if <span style="color:#006600; font-weight:bold;">&#40;</span>strpos <span style="color:#006600; font-weight:bold;">&#40;</span>$text, $search<span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#800000;color:#800000;">50</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return str_replace <span style="color:#006600; font-weight:bold;">&#40;</span>$search, $replacement, $text<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> else <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return $text;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return array_map <span style="color:#006600; font-weight:bold;">&#40;</span>$map, $array<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这个patch由Christian Seiler, Dmitry Stogov提供，目前已经加入到php 5.3。</p>
<p>原文见：<a href="http://wiki.php.net/rfc/closures">Request for Comments: Lambda functions and closures</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/463/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>最简便的清空memcache的方法</title>
		<link>http://www.ooso.net/archives/462</link>
		<comments>http://www.ooso.net/archives/462#comments</comments>
		<pubDate>Sun, 20 Jul 2008 01:08:58 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/462</guid>
		<description><![CDATA[如果要清空memcache的items，常用的办法是什么？杀掉重启？如果有n台memcache需要重启怎么办？挨个做一遍？
很简单，假设memcached运行在本地的11211端口，那么跑一下命令行：
$ echo ”flush_all” &#124; nc localhost 11211
注：flush并不会将items删除，只是将所有的items标记为expired。

				<span class="readmore"><a href="http://www.ooso.net/archives/462" title="最简便的清空memcache的方法">阅读全文（210字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>如果要清空memcache的items，常用的办法是什么？杀掉重启？如果有n台<a href="/?tag=memcache">memcache</a>需要重启怎么办？挨个做一遍？</p>
<p>很简单，假设memcached运行在本地的11211端口，那么跑一下命令行：<br />
<code>$ echo ”flush_all” | nc localhost 11211</code></p>
<p>注：flush并不会将items删除，只是将所有的items标记为expired。</p>
<p>原文：<a href="http://willj.net/blog/2008/06/10/flushing-memcached-servers-the-easy-way/">Flushing memcached servers the easy way</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/462/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>memcache遭遇out of memory错误</title>
		<link>http://www.ooso.net/archives/444</link>
		<comments>http://www.ooso.net/archives/444#comments</comments>
		<pubDate>Tue, 15 Jul 2008 09:34:24 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/444</guid>
		<description><![CDATA[今天在服务器上碰到memcache的out of memory错误，这还是第一次遇到，稍稍有些慌。一共有15台服务器，每台服务器分配了1G内存给memcache，合计有15个G，遇到错误的时候，大概只使用了4个G不到的内存。
现象比较很灵异，设置一个很小的value的时候就会出现这个错误
[root@slave1 bin]# telnet localhost 11211
Trying 127.0.0.1...

				<span class="readmore"><a href="http://www.ooso.net/archives/444" title="memcache遭遇out of memory错误">阅读全文（1214字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>今天在服务器上碰到<a href="/?tag=memcache">memcache</a>的out of memory错误，这还是第一次遇到，稍稍有些慌。一共有15台服务器，每台服务器分配了1G内存给memcache，合计有15个G，遇到错误的时候，大概只使用了4个G不到的内存。</p>
<p>现象比较很灵异，设置一个很小的value的时候就会出现这个错误<br />
<code>[root@slave1 bin]# telnet localhost 11211<br />
Trying 127.0.0.1...<br />
Connected to localhost (127.0.0.1).<br />
Escape character is '^]'.<br />
set key1 0 0 2<br />
SERVER_ERROR out of memory</code></p>
<p>如果设置一个较大的value，反而Stored成功。</p>
<p>翻看了几篇关于memcache内存分配的介绍，大概有一些了解。</p>
<ul>
<li><a href="http://tech.idv2.com/2008/07/11/memcached-002/">memcached全面剖析–2.理解memcached的内存存储</a></li>
<li><a href="http://kevinminnick.blogspot.com/2006/01/understanding-memcached-memory.html">Understanding memcached memory management</a></li>
</ul>
<p>其中后一篇有实际操作的介绍，更为实用。根据个人理解先写个大概。</p>
<p>Memcache为了避免产生内存碎片，采用Slab Allocation机制来整理内存。比如某前端机分配了1G内存给memcache，那么它会将内存按照不同尺寸的小块(chunk)分成若干个组(classes)，组的数目不是很固定，每个组分配1M内存。见下图：<br />
<img src="http://tech.idv2.com/wp-content/uploads/2008/07/memcached-0002-01.png" /></p>
<p>当一条记录插入memcache中的时候，首先会寻找一个合适的能足够容纳这条记录的class。比如你insert一条100字节的数据，它会使用128字节的class来存储这条记录，只有28字节被浪费。</p>
<p>如果用memcached-tool来查看memcache的使用情况，会看到类似下面列出的情况。</p>
<pre>
[root@sql-slave1 bin]# ./memcached-tool localhost
# Item_Size 	Max_age		1MB_pages	Full?
6 64 B 		0 s 		0 		no
7 128 B 	742970 s 	4 		yes
8 256 B		1174852 s 	2 		yes
9 512 B		1279658 s 	1 		yes
10 1 kB		6494118 s 	1 		yes
11 2 kB 	434918 s 	71 		yes
12 4 kB 	917228 s 	140 	yes
13 8 kB 	794192 s 	30 		no
14 16 kB	1373208 s 	6 		yes
15 B 		0 s 		0 		yes
16 B 		0 s 		0 		yes
</pre>
<p>此时插入一条很小的记录，比如只有2个字节，那么memcache会尝试用最小的class（64字节）来保存这条记录，这时候由于64字节的class已满或未分配内存，写入数据的时候就会遭遇到前面所说的<b>out of memcache</b>错误了。</p>
<p>更糟糕的是，这时候增加cache大小似乎也不能解决问题。可行的办法是利用memcached-tool对内存进行移动操作：<br />
<code>[root@sql-slave1 bin]# ./memcached-tool localhost move 14 6</code><br />
上面的操作会移动class 14的一个page到class 6。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/444/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Memcache的备忘</title>
		<link>http://www.ooso.net/archives/428</link>
		<comments>http://www.ooso.net/archives/428#comments</comments>
		<pubDate>Fri, 20 Jun 2008 08:26:43 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/428</guid>
		<description><![CDATA[把memcache使用时的一些细节记录下来.

用memcache保存session的例子，非常简单
PLAIN TEXT

				<span class="readmore"><a href="http://www.ooso.net/archives/428" title="Memcache的备忘">阅读全文（732字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>把memcache使用时的一些细节记录下来.</p>
<ul>
<li>用memcache保存session的例子，非常简单
<div class="igBar"><span id="lcode-98"><a href="#" onclick="javascript:showCodeTxt('code-98'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-98">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$session_save_path = <span style="color:#CC0000;">"tcp://$host:$port?persistent=1&amp;weight=2&amp;timeout=2&amp;retry_interval=10,&nbsp; ,tcp://$host:$port&nbsp; "</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ini_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'session.save_handler'</span>, <span style="color:#CC0000;">'memcache'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ini_set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'session.save_path'</span>, $session_save_path<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
<li>memcache每一个item上限是1M，注意不要超出上限</li>
<li>memcache本身并不支持namespace，但是可以通过一些手段模拟出namespace的效果来，见<a href="http://avenger.name/blog/memcache-namespace/">Memcache 中模拟 namespace</a></li>
<li>刚接触memcache的时候，可能会写出这样的代码来
<div class="igBar"><span id="lcode-99"><a href="#" onclick="javascript:showCodeTxt('code-99'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-99">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$zhang = $memcache-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'key1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$li = $memcache-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'key2'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$wang = $memcache-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'key3'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>这种写法实际运行效果是
<ul>
<li>get(key1) - 客户端发出请求 - 服务器端查询 - 客户端获取</li>
<li>get(key2) - 客户端 - 服务器端 - 客户端</li>
<li>get(key3) - 客户端 - 服务器端 - 客户端</li>
<li>...</li>
</ul>
<p>如此一来，会有三次客户端和服务器端交互的过程。但是如果用批量查询的方法，就只有一次交互的过程。比如:
<div class="igBar"><span id="lcode-100"><a href="#" onclick="javascript:showCodeTxt('code-100'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-100">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$all = $memcache-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>array<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'key1'</span>, <span style="color:#CC0000;">'key2'</span>, <span style="color:#CC0000;">'key3'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>这样性能会有一些提升。对于其它程序语言来说，也封装了类似get_multi这样的方法。
</li>
<li>从数据库从查询获得一个列表，放到memcache里面保存起来是一个不错的主意，但是不要忘记memcache有1m限制。如果列表太大，可以考虑把数据分割开来，然后用key序列来保存这个列表数据，比如event_0_500来保存前500行,用event_0_1000保存500-1000行，在获取的时候可以用前面说的批量get来一次性得到这个列表。</li>
<li>经常<a href="http://www.ooso.net/index.php/archives/414">观察memcache的大小，以及命中率</a>，方便调整缓存策略</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/428/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>FirePHP &#8212; php版的firebug</title>
		<link>http://www.ooso.net/archives/424</link>
		<comments>http://www.ooso.net/archives/424#comments</comments>
		<pubDate>Tue, 17 Jun 2008 07:26:45 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[firefox]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[firebug]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=424</guid>
		<description><![CDATA[在friendfeed上找到的了这个名为FirePHP的Firefox插件，是基于Firebug的一个扩展，可以用来在Firebug的console中方便的输出php的调试信息又不影响php程序的正常运行。实际上这东东出来的时间也不短了，只是以前没试用过，现在把玩了一会也挺有趣。附图一张：

FirePHP包含两个部分


				<span class="readmore"><a href="http://www.ooso.net/archives/424" title="FirePHP &#8212; php版的firebug">阅读全文（445字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在<a href="http://friendfeed.com">friendfeed</a>上找到的了这个名为<a href=" http://www.firephp.org/">FirePHP</a>的Firefox插件，是基于<a href="http://www.ooso.net/index.php/archives/294">Firebug</a>的一个扩展，可以用来在Firebug的console中方便的输出php的调试信息又不影响php程序的正常运行。实际上这东东出来的时间也不短了，只是以前没试用过，现在把玩了一会也挺有趣。附图一张：</p>
<p><img src="http://www.firephp.org/images/Screenshots/Sample1b.png" alt="firephp" /></p>
<h3>FirePHP包含两个部分</h3>
<ul>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/6149">firefox扩展 </a></li>
<li><a href="http://www.firephp.org/DownloadRelease/FirePHPLibrary-FirePHPCore-0.0.2">php程序库 </a></li>
</ul>
<p>FirePHP的php调试信息都是通过在http头里面添加X-FirePHP-Data信息串来标识，不会直接输出到页面上，这样也就避免对php正常输出产生影响。可以输出的调试信息类型如下:</p>
<ul>
<li>正常的调试字符串，类型有LOG,INFO,WARN,ERROR几种</li>
<li>数组array</li>
<li>object</li>
<li>异常Exception</li>
<li>SQL返回数据</li>
<li>http header</li>
</ul>
<p>如果你像我一样使用的是firefox 3，那么可以<a href="http://www.firephp.org/DownloadRelease/FirePHP-FirefoxExtension-0.1.0.1">点这里下载安装FirePHP  0.1.0.1</a></p>
<p>这个版本只适用于php5，再贴个<a href="http://www.live-my-life-with-yuyi.com/blog/archives/354">网友自力更生的简化版</a>，但是功能上就弱多了，而且是通过js来输出调试信息，实用性也不强。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/424/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>在类中获得当前class名称</title>
		<link>http://www.ooso.net/archives/421</link>
		<comments>http://www.ooso.net/archives/421#comments</comments>
		<pubDate>Fri, 13 Jun 2008 01:51:18 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ooso.net/index.php/archives/421</guid>
		<description><![CDATA[php 4.3以后新增了一个魔术常量__CLASS__，用来获得当前class名称。但是在下面的代码中，却不work。
PLAIN TEXT
CODE:


				<span class="readmore"><a href="http://www.ooso.net/archives/421" title="在类中获得当前class名称">阅读全文（693字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="/?tag=php">php</a> 4.3以后新增了一个魔术常量__CLASS__，用来获得当前class名称。但是在下面的代码中，却不work。</p>
<div class="igBar"><span id="lcode-104"><a href="#" onclick="javascript:showCodeTxt('code-104'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-104">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;function showclass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; echo __CLASS__;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class B extends A <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">A::<span style="">showclass</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">B::<span style="">showclass</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这段代码会打出两个A，而不是预想中的AB。</p>
<p>下面这段代码，利用debug_backtrace来获得当前class名，可惜只能在php4上面work，到了php5以上，测试结果全部都是AA-___-</p>
<div class="igBar"><span id="lcode-105"><a href="#" onclick="javascript:showCodeTxt('code-105'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-105">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;function showclass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; $backtrace = debug_backtrace<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; echo $backtrace<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'class'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class B extends A <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>补充一个可以在php4/php5下work的代码</p>
<div class="igBar"><span id="lcode-106"><a href="#" onclick="javascript:showCodeTxt('code-106'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-106">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class A <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;function showclass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $backtrace = debug_backtrace<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; echo <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>int<span style="color:#006600; font-weight:bold;">&#41;</span>PHP_VERSION&gt; <span style="color:#800000;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; get_class<span style="color:#006600; font-weight:bold;">&#40;</span>$backtrace<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'object'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> : $backtrace<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'class'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class B extends A <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>再补充一点，如果需要用静态方法获得class名，请等待php 5.3，见<br />
<a href="http://cvs.php.net/viewvc.cgi/ZendEngine2/tests/lsb_004.phpt?revision=1.1.2.2&#038;pathrev=PHP_5_3">http://cvs.php.net/viewvc.cgi/ZendEngine2/tests/lsb_004.phpt?revision=1.1.2.2&#038;pathrev=PHP_5_3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/421/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
