<?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>Zen Space &#187; admin</title>
	<atom:link href="http://reeze.cn/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://reeze.cn</link>
	<description>php erlang javascript ruby python web linux mac os x</description>
	<lastBuildDate>Fri, 02 Jul 2010 07:19:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>基于var_export 和 include返回值的缓存方案</title>
		<link>http://reeze.cn/2009/07/02/a-cache-system-using-var_export-and-includes-return-value/</link>
		<comments>http://reeze.cn/2009/07/02/a-cache-system-using-var_export-and-includes-return-value/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 04:55:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://reeze.yo2.cn/?p=32980</guid>
		<description><![CDATA[前一篇文章我们研究了include调用返回值的问题，并指出可以通过这种方式来完成序列化相同的功能，现在我就来研究一下这种方法的可行性和效率，因为直接的返回php值肯定是比unserialize()函数要快。
第一步我们来研究下怎么将php对象持久化的保存起来。下面是我定义的一些变量：
123456private $_var;
public $pub = array&#40;'pub value', 3, 4&#41;;
public function __constructor&#40;$var&#41;
&#123;
$this-&#38;gt;_var = $var;
&#125;
12345678910public function show&#40;&#41;
&#123;
echo $this-&#38;gt;_var;
&#125;
&#125;

$string = &#34;It's a string...&#34;;
$array = array&#40;1, 2, 'key' =&#38;gt; 'value', array&#40;'sub-array'&#41;&#41;;
$number = 135345.55;
$class = new MyClass&#40;'class var'&#41;;
123456//通过serialize()方法我们可以将他们持久化比如：
echo serialize&#40;$string&#41;; //s:16:&#34;It's a string...&#34;;
echo serialize&#40;$array&#41;; //a:4:{i:0;i:1;i:1;i:2;s:3:&#34;key&#34;;s:5:&#34;value&#34;;i:2;a:1:{i:0;s:9:&#34;sub-array&#34;;}}
echo serialize&#40;$number&#41;; //d:135345.5499999999883584678173065185546875;
echo serialize&#40;$class&#41;; //O:7:&#34;MyClass&#34;:2:{s:13:&#34;MyClass_var&#34;;N;s:3:&#34;pub&#34;;a:3:{i:0;s:9:&#34;pub value&#34;;i:1;i:3;i:2;i:4;}}
// 我们可以将这些序列化的结果存到文件中，在需要的时候unserialize()返回得到相应的值，但是现在我不会这么做。
前篇文章提到了通过include返回值来直接取得php值对象，首先我们要把值保存起来，因为我们要通过include来包含它，首先遇到的问题就是我们的序列化函数必须要生成合法的php表达式才行，否则include是无法得到相应的返回值的
比如我们要序列化 字符串 &#8220;abcd&#8221; 我们可以这么做
1234file_puts_content&#40;&#34;data.php&#34;, &#34;return 'abcd';&#34;&#41;;
//然后这样取得相应的值
$string = include &#34;data.php&#34;;
echo $string; // 它应该输出 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://reeze.yo2.cn/2009/07/01/php-include-function-return-value-and-method-of-void-serialize/">前一篇文章</a>我们研究了include调用返回值的问题，并指出可以通过这种方式来完成序列化相同的功能，现在我就来研究一下这种方法的可行性和效率，因为直接的返回php值肯定是比unserialize()函数要快。</p>
<div>第一步我们来研究下怎么将php对象持久化的保存起来。下面是我定义的一些变量：</div>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_var</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$pub</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pub value'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __constructor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>_var <span style="color: #339933;">=</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>_var<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;It's a string...&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'value'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sub-array'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color:#800080;">135345.55</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class var'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//通过serialize()方法我们可以将他们持久化比如：</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//s:16:&quot;It's a string...&quot;;</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//a:4:{i:0;i:1;i:1;i:2;s:3:&quot;key&quot;;s:5:&quot;value&quot;;i:2;a:1:{i:0;s:9:&quot;sub-array&quot;;}}</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//d:135345.5499999999883584678173065185546875;</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//O:7:&quot;MyClass&quot;:2:{s:13:&quot;MyClass_var&quot;;N;s:3:&quot;pub&quot;;a:3:{i:0;s:9:&quot;pub value&quot;;i:1;i:3;i:2;i:4;}}</span><br />
<span style="color: #666666; font-style: italic;">// 我们可以将这些序列化的结果存到文件中，在需要的时候unserialize()返回得到相应的值，但是现在我不会这么做。</span></div></td></tr></tbody></table></div>
<p>前篇文章提到了通过include返回值来直接取得php值对象，首先我们要把值保存起来，因为我们要通过include来包含它，首先遇到的问题就是我们的序列化函数必须要生成合法的php表达式才行，否则include是无法得到相应的返回值的<br />
比如我们要序列化 字符串 &#8220;abcd&#8221; 我们可以这么做</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">file_puts_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;data.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;return 'abcd';&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//然后这样取得相应的值</span><br />
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;data.php&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 它应该输出 abcd</span></div></td></tr></tbody></table></div>
<p>那数组怎么办呢？比如上面的数组。我们可以自己编写这个序列化函数</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'array('</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$code</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$key</span>'=&amp;gt;&quot;</span><span style="color: #339933;">.</span>encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">','</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/chop"><span style="color: #990000;">chop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$code</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//remove unnecessary coma</span><br />
<span style="color: #000088;">$code</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$code</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_string"><span style="color: #990000;">is_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$var</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_bool"><span style="color: #990000;">is_bool</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span> ? <span style="color: #0000ff;">'TRUE'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'FALSE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_numeric"><span style="color: #990000;">is_numeric</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$var</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">else</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'NULL'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这个函数可以将字符串，数组以及数字变成合法的php表达式。<br />
比如：<br />
file_put_contents(&#8220;data.php&#8221;, &#8220;&lt;?phpn return &#8221; . encode($array) . &#8220;;n&#8221;);<br />
data.php文件的结果是：</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'value'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'sub-array'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> int<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> int<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;sub-array&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>我们的目的达到了。可以直接的通过include这个文件来得到我们的值。<br />
但是现在有个问题，我们没有序列化对象类型的值，这个该怎么处理呢？<br />
一个类对象有对象的状态和对象的行为，行为在类定义完以后就确定了，所以每个类的实例的行为都是一样的。所以我们可以不考虑，我们只需要考虑类对象的状态就可以了，简单来讲就是类的属性状态需要保存起来。那怎么样得到一个类的属性呢？<br />
经过一番搜寻以后发现一个函数<br />
get_object_vars<br />
(PHP 4, PHP 5)<span style="font-family: monospace;">get_object_vars &#8212; 返回由对象属性组成的关联数组</span><br />
这个函数可以获得对象的属性关联数组，也就只可以得到对象的状态，但是对象的属性有各种访问控制，get_object_vars()函数在对象外访问只能得到对象的公开属性，而无法得到私有属性，这样的话我们就无法得到对象的全部状态，不可行，但是在对象内可以得到对象的所有属性，那我们可不可以在对象内定义一个___get_properties()方法来返回这些状态呢。<br />
给类增加这样一个方法</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get_properties<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> get_obj_vars<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这样我们就可以得到类的所有属性了。第一步算是完成了，我们得到状态该怎么重新恢复出来呢？要在对象外部给对象设置属性我们只有两种情况：一种是这个属性是公开属性，我们可以直接赋值 比如： $obj-&gt;prop = $value; 如果是私有属性我们则需要自己增加setter方法比如 setProp($value);方法，来设置。但是这就会遇到一个问题，我们的属性要么是公开属性，要么必须要有setter方法来设置，很多情况下我们不希望给类增加这么多没有实际用处的方法，也为了封装性，不会有这么多的setter方法。虽然我们能得到对象的状态，但是却无法恢复对象状态，这样的话，我们的序列化方法也就没有什么意义了。我们探索到现在算是失败了。<br />
解决办法：var_export()函数。<br />
在看symfony代码的时候发现了这个函数，手册是这么描述的：<br />
&#8212;&#8212;&#8212;<br />
var_export<br />
(PHP 4 &gt;= 4.2.0, PHP 5)<span style="font-family: monospace;">var_export &#8212; 输出或返回一个变量的字符串表示<br />
描述<br />
mixed var_export ( mixed expression [, bool return] )</span></p>
<p>此函数返回关于传递给该函数的变量的结构信息，它和 var_dump() 类似，不同的是其返回的表示是合法的 PHP 代码。</p>
<p>您可以通过将函数的第二个参数设置为 TRUE，从而返回变量的表示。</p>
<p>这个就是我们想要的那个函数,我们来看看这个函数是怎么使用的。</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//变量继续使用上面定义的变量</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//'It's a string...'</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* array (0 =&amp;gt; 1, 1 =&amp;gt; 2, 'key' =&amp;gt; 'value', 2 =&amp;gt; array (0 =&amp;gt; 'sub-array', ),)*/</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 135345.55</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* MyClass::__set_state(array('_var' =&amp;gt; NULL, 'pub' =&amp;gt; array (0 =&amp;gt; 'pub value', 1 =&amp;gt; 3, 2 =&amp;gt; 4)) */</span></div></td></tr></tbody></table></div>
<p>我们可以看到生成的都是合法的PHP表达式。通过设置第二个参数为true，就可以将返回结果赋值给变量比如$new_array_string = var_export($array, TRUE);然后将这个结果写入文件持久化</p>
<p>file_put_contents(&#8220;data.php&#8221;, &#8220;public function __constructor($var)<br />
{<br />
$this-&gt;_var = $var;<br />
}</p>
<p>public function show()<br />
{<br />
echo $this-&gt;_var;<br />
}<br />
public static function __set_state(array $array)<br />
{<br />
$tmp = new MyClass();<br />
foreach($array as $key =&gt; $value)<br />
{<br />
$this-&gt;$key = $value;<br />
}<br />
}<br />
}</p>
<p>// 一些变量<br />
$string = &#8220;It&#8217;s a string&#8230;&#8221;;<br />
$array = array(1, 2, &#8216;key&#8217; =&gt; &#8216;value&#8217;, array(&#8217;sub-array&#8217;));<br />
$number = 135345.55;<br />
$class = new MyClass(&#8216;class var&#8217;)</p>
<p>// 缓存<br />
cache(&#8220;string.data.php&#8221;, $string); // 当然扩展名不一定非得php， 文件名我也只是简单的处理<br />
cache(&#8220;array.data.php&#8221;, $array);<br />
// 等等。。。</p>
<p>// 获取数据,当然也可以在其他文件中来获取。<br />
$class = cache_get(&#8220;class.data.php&#8221;);</p>
<p>参考：http://www.thoughtlabs.com/2008/02/02/phps-mystical-__set_state-method/?dsq=12016456</p>
]]></content:encoded>
			<wfw:commentRss>http://reeze.cn/2009/07/02/a-cache-system-using-var_export-and-includes-return-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gmail新功能之标签改进</title>
		<link>http://reeze.cn/2009/07/02/gmail%e6%96%b0%e5%8a%9f%e8%83%bd%e4%b9%8b%e6%a0%87%e7%ad%be%e6%94%b9%e8%bf%9b/</link>
		<comments>http://reeze.cn/2009/07/02/gmail%e6%96%b0%e5%8a%9f%e8%83%bd%e4%b9%8b%e6%a0%87%e7%ad%be%e6%94%b9%e8%bf%9b/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 02:27:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[G-related]]></category>

		<guid isPermaLink="false">http://reeze.yo2.cn/?p=32974</guid>
		<description><![CDATA[今天刚登陆gmail，又发现新的惊喜：

很久以前gmail就支持了给邮件加标签的功能，但是到后面基本没有用到，主要有两个原因： 加标签麻烦，以前标签的位置有点低，即使是拖到最上面还是很低，比较麻烦，二是因为以前给邮件加标签似乎只能通过菜单栏来加标签，现在可以直接通过拖放给邮件加标签，如下图：

现在标签的位置和收件箱紧靠着，同时可以把不常用的标签隐藏起来，标签可以重排序，gmail的beta是实实在在的beta，永远不满足，我喜欢：）


]]></description>
			<content:encoded><![CDATA[<p>今天刚登陆gmail，又发现新的惊喜：</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-32978" title="new" src="http://image-001.yo2cdn.com/wp-content/uploads/362/36227/2009/07/new.png" alt="new" width="610" height="334"></p>
<p>很久以前gmail就支持了给邮件加标签的功能，但是到后面基本没有用到，主要有两个原因： 加标签麻烦，以前标签的位置有点低，即使是拖到最上面还是很低，比较麻烦，二是因为以前给邮件加标签似乎只能通过菜单栏来加标签，现在可以直接通过拖放给邮件加标签，如下图：</p>
<p style="text-align: center;"><img class="size-full wp-image-32975 aligncenter" title="label" src="http://image-001.yo2cdn.com/wp-content/uploads/362/36227/2009/07/label.png" alt="label" width="251" height="461"></p>
<p style="text-align: left;">现在标签的位置和收件箱紧靠着，同时可以把不常用的标签隐藏起来，标签可以重排序，gmail的beta是实实在在的beta，永远不满足，我喜欢：）</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-32977" title="1" src="http://image-001.yo2cdn.com/wp-content/uploads/362/36227/2009/07/1.png" alt="1" width="279" height="412"></p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://reeze.cn/2009/07/02/gmail%e6%96%b0%e5%8a%9f%e8%83%bd%e4%b9%8b%e6%a0%87%e7%ad%be%e6%94%b9%e8%bf%9b/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP中include()函数(以及相关函数)的返回值及避免序列化开销的方法</title>
		<link>http://reeze.cn/2009/07/01/a-way-use-include-to-avoid-unserialize/</link>
		<comments>http://reeze.cn/2009/07/01/a-way-use-include-to-avoid-unserialize/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 10:24:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://reeze.yo2.cn/?p=32949</guid>
		<description><![CDATA[以前乱翻symfony生成的缓存文件的时候看到很多类似：
1&#60;?php return array&#40;'key' =&#62; &#34;value&#34;&#41;; ?&#62;
这种表达式，当时并没有怎么在意，今天研究symfony代码的时候看到这样一句代码
1&#60;?php &#160;$this-&#62;classes = include&#40;$file&#41;; ?&#62;;
一直都是通过include require来包含文件，但是从来没有使用过他的返回值，印象中include返回的无非是true或者false吧。今天自己看了下文档，发现在php文件中是可以直接调用return的。比如：
12345678910// return.php
&#60;?php
$value = array&#40;'haha', 1, 3&#41;;
return $value;
?&#62;
// get_return.php
&#60;?php
$value = include&#40;&#34;return.php&#34;&#41;;&#60;/span&#62;&#60;/p&#62;
echo $value; // 输出 Array, 因为$value 是从return.php返回的一个数组
?&#62;
其实项目中很少情况需要这样的返回方法。如果想要从return.php中得到返回值一般是通过调用return.php中所调用的函数来得到。
在symfony中这种方式就很合理，如果大家熟悉symfony的话，应该知道，symfony运行起来以后会在cache目录下生成系统配置文件的缓存 ，诚然可以通过序列化的方式来缓存这些信息，但是反序列化是需要消耗资源的。通过这种方式来做持久化也是个不错的选择.
]]></description>
			<content:encoded><![CDATA[<p>以前乱翻symfony生成的缓存文件的时候看到很多类似：</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>这种表达式，当时并没有怎么在意，今天研究symfony代码的时候看到这样一句代码</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">classes</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>;</div></td></tr></tbody></table></div>
<p>一直都是通过include require来包含文件，但是从来没有使用过他的返回值，印象中include返回的无非是true或者false吧。今天自己看了下文档，发现在php文件中是可以直接调用return的。比如：</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:97%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">// return.php<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'haha'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
// get_return.php<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;return.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 输出 Array, 因为$value 是从return.php返回的一个数组</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p><span style="font-family: monospace;">其实项目中很少情况需要这样的返回方法。如果想要从return.php中得到返回值一般是通过调用return.php中所调用的函数来得到。</span></p>
<p><span style="font-family: monospace;">在symfony中这种方式就很合理，如果大家熟悉symfony的话，应该知道，symfony运行起来以后会在cache目录下生成系统配置文件的缓存 ，诚然可以通过序列化的方式来缓存这些信息，但是反序列化是需要消耗资源的。通过这种方式来做持久化也是个不错的选择.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://reeze.cn/2009/07/01/a-way-use-include-to-avoid-unserialize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->