<?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>Sat, 07 Aug 2010 00:23:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>php实现的thrift socket server</title>
		<link>http://www.ooso.net/archives/537</link>
		<comments>http://www.ooso.net/archives/537#comments</comments>
		<pubDate>Fri, 25 Jun 2010 08:03:31 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=537</guid>
		<description><![CDATA[这些天用php写了个thrift的socket server，因为原来thrift的源码里php部分只有基于apache的服务器端代码，再加上前些日子看到php也能直接使用libevent构建web服务器，所以才会想到写这个玩玩。
php-thrift-server源码
代码直接从apache的thrift项目clone过来，托管在github上：
http://github.com/volca/thrift

				<span class="readmore"><a href="http://www.ooso.net/archives/537" title="php实现的thrift socket server">阅读全文（2531字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>这些天用php写了个thrift的socket server，因为原来thrift的源码里php部分只有基于apache的服务器端代码，再加上前些日子看到<a href="/tag/php">php</a>也能直接使用libevent构建web服务器，所以才会想到写这个玩玩。</p>
<h2>php-thrift-server源码</h2>
<p>代码直接从apache的thrift项目clone过来，托管在github上：</p>
<p><a href="http://github.com/volca/thrift">http://github.com/volca/thrift</a></p>
<p>新增或改动的代码如下:</p>
<ol>
<pre>
lib/php/
`-- src
    |-- server
    |   |-- TNonblockingServer.php
    |   `-- TServer.php
    `-- transport
        |-- TNonblockingServerSocket.php
        |-- TNonblockingSocket.php
        |-- TServerSocket.php
        |-- TServerTransport.php
test/php
|-- TestClient.php
|-- TestNonblockingServer.php
</pre>
</ol>
<h2>使用示例</h2>
<p>获取thrift的源码，并编译出thrift工具，编译过程请搜索<br />
<code><br />
git clone git://github.com/volca/thrift.git<br />
</code></p>
<p>安装php,以及apc, libevent扩展:<br />
<code><br />
pecl install apc<br />
#需要先libevent-devel之类的包包<br />
pecl install libevent<br />
</code></p>
<p>运行php的socket服务器，我直接从thrift的test代码中修改了一个独立运行的php server，见thrift/test/php/TestNonblockingServer.php，这里也包含一个测试业务代码的实现。<br />
<code><br />
cd thrift/test/php<br />
#用thrift命令行工具生成php的测试类库<br />
make<br />
#启动thrift服务，会监听本机的9090端口<br />
php TestNonblockingServer.php<br />
</code></p>
<p>客户端的代码也一并提供，对各种数据类型比如int, float, string, list等等进行测试。<br />
<code><br />
php TestClient.php<br />
</code></p>
<h2>性能测试</h2>
<p>apache + php的测试结果<br />
<code>testVoid() = void<br />
testString("Test") = "Test"<br />
testByte(1) = 1<br />
testI32(-1) = -1<br />
testI64(-34359738368) = -34359738368<br />
testDouble(-852.234234234) = -852.234234234<br />
testStruct({"Zero", 1, -3, -5}) = {"Zero", 1, -3, -5}<br />
testNest({1, {"Zero", 1, -3, -5}), 5} = {1, {"Zero", 1, -3, -5}, 5}<br />
testMap({0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}) = {0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}<br />
testSet({-2, -1, 0, 1, 2}) = {1, 1, 1, 1, 1}<br />
testList({-2, -1, 0, 1, 2}) = {-2, -1, 0, 1, 2}<br />
testEnum(ONE) = 1<br />
testEnum(TWO) = 2<br />
testEnum(THREE) = 3<br />
testEnum(FIVE) = 5<br />
testEnum(EIGHT) = 8<br />
testTypedef(309858235082523) = 309858235082523<br />
Total time: 41 ms<br />
</code><br />
php + libevent的socket server测试结果<br />
<code>testVoid() = void<br />
testString("Test") = "Test"<br />
testByte(1) = 1<br />
testI32(-1) = -1<br />
testI64(-34359738368) = -34359738368<br />
testDouble(-852.234234234) = -852.234234234<br />
testStruct({"Zero", 1, -3, -5}) = {"Zero", 1, -3, -5}<br />
testNest({1, {"Zero", 1, -3, -5}), 5} = {1, {"Zero", 1, -3, -5}, 5}<br />
testMap({0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}) = {0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}<br />
testSet({-2, -1, 0, 1, 2}) = {1, 1, 1, 1, 1}<br />
testList({-2, -1, 0, 1, 2}) = {-2, -1, 0, 1, 2}<br />
testEnum(ONE) = 1<br />
testEnum(TWO) = 2<br />
testEnum(THREE) = 3<br />
testEnum(FIVE) = 5<br />
testEnum(EIGHT) = 8<br />
testTypedef(309858235082523) = 309858235082523<br />
Total time: 8 ms<br />
</code></p>
<p>这个测试中，没有耗时很长的请求，处理逻辑完全一样，php socket server耗时仅为apache + php的五分之一。</p>
<h2>thrift是什么？</h2>
<p>thrift流传的似乎不是太广泛，而且有被别的技术替代的趋势，所以下面还是引用一下别的文章的介绍：</p>
<blockquote><p>Thrift由一个软件库和一系列的代码生成工具组成，由 Facebook开发。目的是为了加快软件开发和实现高效和可扩展的后台服务。主要目标是不同程序开语言之间实现高效和可靠的通信，这需要将不同语言之间抽象出一个通用层，然后由不同语言来实现这个通用层。在这里要特别指出的是，Thrift允许开发人员定义数据类型和服务接口（定义在一个中性语言文件里），并通过这个文件生成构建RPC客户端和服务端所需的代码。</p>
<p>简单分析其机理，Thrift就是实现C/S模式，通过代码生成工具将接口定义文件生成服务器端和客户端代码（可以为不同语言），从而实现服务端和客户端跨语言的支持。</p>
<p>Thrift可以分为传输层和协议层：</p>
<p>传输层定义了数据的传输方式，可以为TCP/IP传输，内存共享或者文件共享等形式；<br />
协议层定义了数据的传输格式，可以为二进制流或者XML等形式。<br />
当服务器端使用socket协议时，可以用simple|thread-pool|threaded|nonblocking等方式运行，从而获得更好的性能。
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/537/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>php的filter扩展小技巧</title>
		<link>http://www.ooso.net/archives/559</link>
		<comments>http://www.ooso.net/archives/559#comments</comments>
		<pubDate>Fri, 30 Apr 2010 11:29:26 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=559</guid>
		<description><![CDATA[做为一个合格的web开发人员，一定会牢记一个原则——永远不能相信用户输入的数据，行走江湖，安全第一是很重要的。用户通过表单或url传过来的数据，一定要仔细检查过了，才往后台数据库里存进去。在一个成熟的开发团队里，贯彻这个原则不成问题；但是如果在一个新人老手混搭的小team里，很容易就忽视了这个问题，那么各种安全漏洞比如跨站攻击，sql注入等等真是防不胜防。
实际上，用php 5自带的filter扩展能够较好的解决这个问题。我在从前的blog里记录了filter扩展的常规用法——直接利用filter来校验数据，这样有不少额外的代码量，所以我得介绍一个比较偷懒的办法——自动对所有输入变量进行过滤，这只需要对php.ini增加一行配置，然后重启apache或fastcgi让php配置生效。

				<span class="readmore"><a href="http://www.ooso.net/archives/559" title="php的filter扩展小技巧">阅读全文（890字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>做为一个合格的web开发人员，一定会牢记一个原则——永远不能相信用户输入的数据，行走江湖，安全第一是很重要的。用户通过表单或url传过来的数据，一定要仔细检查过了，才往后台数据库里存进去。在一个成熟的开发团队里，贯彻这个原则不成问题；但是如果在一个新人老手混搭的小team里，很容易就忽视了这个问题，那么各种安全漏洞比如跨站攻击，sql注入等等真是防不胜防。</p>
<p>实际上，用php 5自带的filter扩展能够较好的解决这个问题。我在<a href="http://www.ooso.net/archives/204">从前的blog里记录了filter扩展的常规用法</a>——直接利用filter来校验数据，这样有不少额外的代码量，所以我得介绍一个比较偷懒的办法——自动对所有输入变量进行过滤，这只需要对php.ini增加一行配置，然后重启apache或fastcgi让php配置生效。</p>
<blockquote><p>filter.default="special_chars"</p></blockquote>
<p>开启了这项配置后，会自动使用<a href="http://php.net/filter_input">filter_input</a>方法对$_GET, $_POST, $_COOKIE, $_REQUEST以及$_SERVER变量进行过滤转义。配置中special_chars是常量FILTER_SANITIZE_SPECIAL_CHARS的缩写，它能自动转义大部分危险字符例如： <strong>&#39;&quot;&lt;&gt;</strong>。而php手册对它的解释是：</p>
<blockquote><p>HTML-escape '"<>&#038; and characters with ASCII value less than 32, optionally strip or encode other special characters. </p></blockquote>
<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:#0000FF;">$foo</span> = <span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'foo'</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;"><span style="color:#000066;">echo</span> <span style="color:#0000FF;">$foo</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在部分场合，我们可能还是需要未转义的变量，比如某个ajax接受的参数是一段json串，用这段代码即可获得原始数据：</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:#0000FF;">$foo</span> = filter_input <span style="color:#006600; font-weight:bold;">&#40;</span>INPUT_GET, <span style="color:#FF0000;">'foo'</span>,&nbsp; FILTER_UNSAFE_RAW<span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>fitler扩展与yahoo使用的yiv如出一辙，印象里似乎就是yahoo对yiv做了些修改贡献给php社区，但是暂时没找到出处。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/559/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>关于“facebook的memcached实战”小记</title>
		<link>http://www.ooso.net/archives/558</link>
		<comments>http://www.ooso.net/archives/558#comments</comments>
		<pubDate>Thu, 29 Apr 2010 16:38:07 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=558</guid>
		<description><![CDATA[上周挤到QCon的会场里，听了两场 —— Facebook的Memcached实战，以及Twitter 的可伸缩性数据架构。当时对facebook超大规模使用memcached印象很深刻，只可惜到现在也没见到这个的ppt。平时用php比较多，因此听闻同样使着php的facebook讲memcached，有些小小的感触，记录下来。
更高效的序列化函数
php有两个memcache扩展，默认都是使用php自带的序列化函数serialize来存储数组或对象。但是serialize最为人诟病的就是速度慢，序列化之后占用空间大。由于facebook已经在memcached里保存了200T字节的数据，因此序列化函数即便作出的百分之一的优化对它来说都是个不小的收益。他们发粪涂墙在thrift的binary协议基础上搞出了一个fb_serialize，据称这个序列化方法能快上3倍，快倒算了，还能节省30%空间， 200T字节的数据能节省出30%，简直就是传说中的银弹啊，这让php官方的开发人员们情何以堪？

				<span class="readmore"><a href="http://www.ooso.net/archives/558" title="关于“facebook的memcached实战”小记">阅读全文（1275字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>上周挤到QCon的会场里，听了两场 —— Facebook的<a href="/tag/memcache">Memcached</a>实战，以及Twitter 的可伸缩性数据架构。当时对facebook超大规模使用memcached印象很深刻，只可惜到现在也没见到这个的ppt。平时用php比较多，因此听闻同样使着php的facebook讲memcached，有些小小的感触，记录下来。</p>
<h2>更高效的序列化函数</h2>
<p>php有两个memcache扩展，默认都是使用php自带的序列化函数serialize来存储数组或对象。但是serialize最为人诟病的就是速度慢，序列化之后占用空间大。由于facebook已经在memcached里保存了200T字节的数据，因此序列化函数即便作出的百分之一的优化对它来说都是个不小的收益。他们发粪涂墙在thrift的binary协议基础上搞出了一个fb_serialize，据称这个序列化方法能快上3倍，快倒算了，还能节省30%空间， 200T字节的数据能节省出30%，简直就是传说中的银弹啊，这让php官方的开发人员们情何以堪？</p>
<p>facebook目前已经开源了thrift，其中自带了一个thrift协议的php扩展，但是这些代码里没有找到传说中的fb_serialize，我倒是从最近他们放出来<a href="http://github.com/facebook/hiphop-php">hiphop-php</a>里找到了这部分代码，哪位大侠去扒拉扒拉弄出来做成php扩展造福广大群众？</p>
<p>作为备选方案，我推荐<a href="/archives/538">igbinary</a>，这也是一个binary的序列化方法。在上次的测试结果中，它甚至能节约50%的存储空间，速度也是稳超php原生的序列化方法，搞不好facebook换了这个序列化方法能省下更多的内存来？</p>
<p>节约每个item的存储空间有什么好处？我个人认为一个是省钱，另外一个就是能够带来速度上的提升。我们平常碰到稍大一点的item都得用gzip压的妥妥贴贴的才送到memcached里，网络传输的开销小了，这是实实在在的性能提升。何乐而不为？</p>
<h2>mcproxy</h2>
<p>mcproxy = memcached + proxy。facebook的机房遍布各洲，利用mcproxy来进行跨机房的同步或分发，全球制霸，指着太阳就能等到那天了。一般的互联网企业还真用不上这玩意，规模还没上去的时候，这些乱七八糟的只会拖后腿。facebook还没开源mcproxy，但是我找到两个替代品：</p>
<ol>
<ul>
<li><a href="http://code.google.com/p/memagent/">memagent</a> is a simple but useful proxy program for memcached servers. </li>
<li><a href="http://code.google.com/p/moxi/">moxi</a> = memcached + integrated proxy</li>
</ul>
</ol>
<p>从项目描述来看，moxi最接近facebook介绍的mcproxy，成熟度也比较高。</p>
<h2>数据的一致性</h2>
<p>Marc Kwiatkowski在会场上用大篇幅的ppt和大量的动画来阐述这个问题，他们用了很多额外的手段来解决在跨机房情况下因为延时问题造成的脏数据。这一段看着挺晕，但是我们联想到facebook用到的多级cache技术: 本地全局变量 + apc + memcache，不难理解这样做颇有些道理，这相当于用memcache实现了一个版本控制系统。</p>
<p>我还是很晕这段ppt。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/558/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>关于改善xhprof使用情况的设想</title>
		<link>http://www.ooso.net/archives/557</link>
		<comments>http://www.ooso.net/archives/557#comments</comments>
		<pubDate>Fri, 23 Apr 2010 00:58:29 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=557</guid>
		<description><![CDATA[自从去年将xhprof用在生产环境以来，对生产环境的程序调试，性能优化都带来很多便利。但是在使用过程中，还是有一些细节需要改善。
问题



				<span class="readmore"><a href="http://www.ooso.net/archives/557" title="关于改善xhprof使用情况的设想">阅读全文（974字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>自从去年<a href="http://www.ooso.net/archives/522">将xhprof用在生产环境</a>以来，对生产环境的程序调试，性能优化都带来很多便利。但是在使用过程中，还是有一些细节需要改善。</p>
<h2>问题</h2>
<ol>
<ul>
<li>xhprof的profile日志直接以文件形式保存在生产服务器上，需要定时清理，或者收集起来移动到查看日志的工具机上。</li>
<li>由于xhprof生成的profile是一个大数组，所以保存到文件时使用了标准的php serialize，日志文件偏大，一个不留神就容易占用很多服务器磁盘空间。</li>
<li>查看日志列表时，一个个点开查看比较费劲。</li>
</ul>
</ol>
<p>针对这几个问题，我有一些小小的设想。</p>
<h2>日志存放</h2>
<p>部署一个中央日志服务器，采用facebook的<a href="http://github.com/facebook/scribe">scribe</a>来收集日志。生产环境的服务器产生的xhprof日志，都写入到scribe的客户端，由客户端自动同步到中央日志服务器的scribe上，不占用本地的存储空间。在代码上的改动也比较小，只要基于iXHProfRuns接口实现一个XhprofRuns类，调整save_run方法的存储方式即可。</p>
<h2>更换序列化方法</h2>
<p>xhprof默认是将profile信息用php原生的序列化方法处理后进行保存，而我在前两天比较过<a href="http://www.ooso.net/archives/538">igbinary vs serialize vs json_encode</a>的性能和占用字节数，这个测试里igbinary在各方面都有一定优势，尤其是占用存储空间会大幅度减小，所以我只要更换序列化方法为igbinary_serialize即可获得改善。</p>
<h2>优化列表展示</h2>
<p>我已经厌倦挨个查看profile日志的大图，费时费力还没有针对性。所以我现在的做法是，在profile日志的列表中将前1000个日志的总体执行时间直接输出到列表中，并且将执行时间过长的日志用红色粗体标识。做了这个小小的改动之后，当我想要去视察一下运行情况时，就把日志列表中那些红通通的链接点开看看就行了，真正的省时省力。</p>
<p>如何从xhprof日志文件中获取执行时间？简单的代码如下</p>
<div class="igBar"><span id="lphp-6"><a href="#" onclick="javascript:showCodeTxt('php-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-6">
<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;">/**</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;"> * 由xhprof日志获得执行时间</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;"> *</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;"> * @param string $log xhprof日志的文件路径</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;"> * @return int 执行时间</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>
<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;">function</span> getSpentTime<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$log</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:#0000FF;">$profile</span> = <span style="color:#000066;">unserialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">file_get_contents</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$log</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; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$profile</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'main()'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'wt'</span><span style="color:#006600; font-weight:bold;">&#93;</span> / <span style="color:#CC66CC;color:#800000;">1000</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/557/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>igbinary vs serialize vs json_encode</title>
		<link>http://www.ooso.net/archives/538</link>
		<comments>http://www.ooso.net/archives/538#comments</comments>
		<pubDate>Sun, 18 Apr 2010 15:01:58 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/archives/538</guid>
		<description><![CDATA[最近看到memcached扩展支持额外的序列化方式 -- igbinary，这是一个未收录到pecl的php扩展，它提供的两个主要方法：


igbinary_serialize

				<span class="readmore"><a href="http://www.ooso.net/archives/538" title="igbinary vs serialize vs json_encode">阅读全文（661字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>最近看到<a href="http://pecl.php.net/memcached">memcached</a>扩展支持额外的序列化方式 -- <a href="http://github.com/phadej/igbinary">igbinary</a>，这是一个未收录到pecl的php扩展，它提供的两个主要方法：
<ol>
<ul>
<li>igbinary_serialize</li>
<li>igbinary_unserialize</li>
</ul>
</ol>
<p>据称可以用它来代替php自带的序列化函数serialize，性能更好，而且占用的字节数也更少。下面我就 igbinary ，serialize ，json_encode三者的性能做了一个简单的测试。</p>
<h2>测试</h2>
<p>以一个包含1000000个元素的数组做为原始数据，分别以json, serialize, igbinary进行序列化和反向操作。</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<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:#000066;">ini_set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'memory_limit'</span>, <span style="color:#FF0000;">'512m'</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;">$array</span> = <span style="color:#000066;">array_fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">0</span>, <span style="color:#CC66CC;color:#800000;">1000000</span>, <span style="color:#000066;">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;">9999</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;</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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$export</span> = json_encode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$array</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'JSON Encode: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$import</span> = json_decode<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$export</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'JSON Decode: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$export</span> = <span style="color:#000066;">serialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$array</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Serialize: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$import</span> = <span style="color:#000066;">unserialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$export</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Serialize: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$export</span> = igbinary_serialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$array</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Igbinary Serialize: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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;">$start</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$import</span> = igbinary_unserialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$export</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;">$end</span> = <span style="color:#000066;">microtime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000000; font-weight:bold;">true</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;">$duration</span> = <span style="color:#0000FF;">$end</span> - <span style="color:#0000FF;">$start</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><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Igbinary Serialize: '</span> . <span style="color:#0000FF;">$duration</span> . PHP_EOL<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:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>测试结果</h2>
<blockquote><p>JSON Encode: 0.084825992584229<br />
JSON Decode: 0.34976410865784<br />
Serialize: 0.38241410255432<br />
Serialize: 7.7904229164124<br />
Igbinary Serialize: 0.046916007995605<br />
Igbinary Serialize: 0.23396801948547</p></blockquote>
<p>从测试结果来看，速度方面优先级排列为 igbinary > json > serialize。同时我们也可以看到，<a href="/tag/php">php</a>原生的serialize在对大对象进行反向操作时，速度真是掉队一大截了。</p>
<p>占用字节数对比</p>
<ol>
<ul>
<li>json: 5000001</li>
<li>serialize: 15888902</li>
<li>igbinary: 7868681</li>
</ul>
</ol>
<p>在没有中文字符的情况下，json胜出，igbinary次之，serialize又被甩了几条街。</p>
<h2>一图顶千言</h2>
<p>柱状图越矮小的性能越好<br />
<img src="http://chart.apis.google.com/chart?cht=bvg&#038;chdl=json|serialize|igbinary&#038;chco=ff0000%2C00ff00%2C0000ff&#038;chs=600x300&#038;chxl=0%3A|Export|Import|1%3A|0|1|2|3|4|5|6|7|8|9|10&#038;chxt=x%2Cy&#038;chbh=25%2C1%2C25&#038;chds=0%2C10%2C0%2C10%2C0%2C10&#038;chd=t%3A0.084%2C0.349|0.382%2C7.79|0.047%2C0.234" alt="igbinary vs serialize vs json_encode 速度比较" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/538/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<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-11"><a href="#" onclick="javascript:showCodeTxt('php-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-11">
<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-12"><a href="#" onclick="javascript:showCodeTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<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-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;">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-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;">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>4</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>8</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-22"><a href="#" onclick="javascript:showCodeTxt('php-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-22">
<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-23"><a href="#" onclick="javascript:showCodeTxt('code-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-23">
<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-24"><a href="#" onclick="javascript:showCodeTxt('code-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-24">
<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-25"><a href="#" onclick="javascript:showCodeTxt('code-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-25">
<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-26"><a href="#" onclick="javascript:showCodeTxt('code-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-26">
<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>20</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>2</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-28"><a href="#" onclick="javascript:showCodeTxt('code-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-28">
<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>13</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-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:#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-33"><a href="#" onclick="javascript:showCodeTxt('php-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-33">
<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-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:#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-38"><a href="#" onclick="javascript:showCodeTxt('php-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-38">
<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-39"><a href="#" onclick="javascript:showCodeTxt('php-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-39">
<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-40"><a href="#" onclick="javascript:showCodeTxt('php-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-40">
<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>4</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-42"><a href="#" onclick="javascript:showCodeTxt('php-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-42">
<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>6</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-45"><a href="#" onclick="javascript:showCodeTxt('code-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-45">
<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-46"><a href="#" onclick="javascript:showCodeTxt('code-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-46">
<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-49"><a href="#" onclick="javascript:showCodeTxt('code-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-49">
<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-50"><a href="#" onclick="javascript:showCodeTxt('code-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-50">
<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-55"><a href="#" onclick="javascript:showCodeTxt('php-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-55">
<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-56"><a href="#" onclick="javascript:showCodeTxt('php-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-56">
<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-57"><a href="#" onclick="javascript:showCodeTxt('php-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-57">
<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-58"><a href="#" onclick="javascript:showCodeTxt('php-58'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-58">
<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-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;">#用户自定义的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-68"><a href="#" onclick="javascript:showCodeTxt('code-68'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-68">
<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-69"><a href="#" onclick="javascript:showCodeTxt('code-69'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-69">
<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-70"><a href="#" onclick="javascript:showCodeTxt('code-70'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-70">
<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-71"><a href="#" onclick="javascript:showCodeTxt('code-71'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-71">
<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-72"><a href="#" onclick="javascript:showCodeTxt('code-72'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-72">
<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-73"><a href="#" onclick="javascript:showCodeTxt('code-73'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-73">
<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-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;">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-82"><a href="#" onclick="javascript:showCodeTxt('code-82'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-82">
<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-83"><a href="#" onclick="javascript:showCodeTxt('code-83'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-83">
<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-84"><a href="#" onclick="javascript:showCodeTxt('code-84'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-84">
<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-85"><a href="#" onclick="javascript:showCodeTxt('code-85'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-85">
<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-86"><a href="#" onclick="javascript:showCodeTxt('code-86'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-86">
<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-87"><a href="#" onclick="javascript:showCodeTxt('code-87'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-87">
<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-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;">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-96"><a href="#" onclick="javascript:showCodeTxt('code-96'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-96">
<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-97"><a href="#" onclick="javascript:showCodeTxt('code-97'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-97">
<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-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;">$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-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;">$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-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;">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-101"><a href="#" onclick="javascript:showCodeTxt('code-101'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-101">
<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-102"><a href="#" onclick="javascript:showCodeTxt('code-102'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-102">
<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>
	</channel>
</rss>
