<?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>花荣老师的博客</title>
	<atom:link href="http://www.masalife.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.masalife.com</link>
	<description>没有什么能够阻挡，我对自由的向往\n天马行空的生涯，我的心了无牵挂。(PS:如果您能看到这段话，请使用chrome浏览器)</description>
	<lastBuildDate>Sun, 05 Sep 2010 03:35:20 +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>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</title>
		<link>http://www.masalife.com/archives/1009</link>
		<comments>http://www.masalife.com/archives/1009#comments</comments>
		<pubDate>Sun, 05 Sep 2010 03:28:07 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=1009</guid>
		<description><![CDATA[原文地址： http://www.xaprb.com/blog/2010/09/04/why-mysql-replication-is-better-than-mysqlbinlog-for-recovery 通常，我们在恢复mysql数据的时候，都是恢复一个完整的备份， 再通过mysqlbinlog来导入此备份之后的二进制日志（binary logs） 这里有另外一个更好的方法: 操作如下： 首先恢复一个完整的备份，假设此服务器为SERVER_A。 然后在另外一台服务器SERVER_B上面，安装一个新的mysql，复制过去二进制日志文件和二进制日志的索引文件(index)，然后启动mysqld,设置为主服务器。 最后把SERVER_A设置为 SERVER_B的从服务器，从第一个二进制日志文件的position 0 开始 replication复制。 为什么这个方法更好呢？ 因为测试证明，replication比mysqlbinlog更加可靠。 比如下面这条语句： insert into tbl(col) values(connection_id()); 使用mysqlbinlog进行恢复，肯定会得到错误的结果。 而且在某些版本的mysql上，BLOB类型的数据无法通过mysqlbinlog恢复。 详见：http://bugs.mysql.com/bug.php?id=33048 而且replication更加灵活，你可以使用start slave until 等语句， 还可以暂时停止复制，继续复制等等。 Related posts:mysql Table Locking Issues mysql 时间函数 mysql grant 语法 discuz7.2勋章模块中的bug PHP和couchDB 转载请注明出处。 © 花荣 for 花荣老师的博客, 2010. &#124; Permalink &#124; No comment &#124; Add to del.icio.us Post tags: mysql IT大腕交流群： 110443867 投稿邮箱： huarong@masalife.com 或者直接联系QQ： 190890101


Related posts:<ol><li><a href='http://www.masalife.com/archives/184' rel='bookmark' title='Permanent Link: mysql Table Locking Issues'>mysql Table Locking Issues</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
<li><a href='http://www.masalife.com/archives/426' rel='bookmark' title='Permanent Link: mysql grant 语法'>mysql grant 语法</a></li>
<li><a href='http://www.masalife.com/archives/238' rel='bookmark' title='Permanent Link: discuz7.2勋章模块中的bug'>discuz7.2勋章模块中的bug</a></li>
<li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址：</p>
<p>http://www.xaprb.com/blog/2010/09/04/why-mysql-replication-is-better-than-mysqlbinlog-for-recovery</p>
<p>通常，我们在恢复mysql数据的时候，都是恢复一个完整的备份，<br />
再通过mysqlbinlog来导入此备份之后的二进制日志（binary logs）<br />
这里有另外一个更好的方法:</p>
<p>操作如下：<br />
首先恢复一个完整的备份，假设此服务器为SERVER_A。<br />
然后在另外一台服务器SERVER_B上面，安装一个新的mysql，复制过去二进制日志文件和二进制日志的索引文件(index)，然后启动mysqld,设置为主服务器。<br />
最后把SERVER_A设置为 SERVER_B的从服务器，从第一个二进制日志文件的position 0 开始 replication复制。</p>
<p>为什么这个方法更好呢？</p>
<p>因为测试证明，replication比mysqlbinlog更加可靠。</p>
<p><del datetime="2010-09-05T03:28:01+00:00">比如下面这条语句：<br />
insert into tbl(col) values(connection_id());<br />
使用mysqlbinlog进行恢复，肯定会得到错误的结果。</p>
<p>而且</del>在某些版本的mysql上，BLOB类型的数据无法通过mysqlbinlog恢复。<br />
详见：http://bugs.mysql.com/bug.php?id=33048</p>
<p>而且replication更加灵活，你可以使用start slave until 等语句，<br />
还可以暂时停止复制，继续复制等等。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/184' rel='bookmark' title='Permanent Link: mysql Table Locking Issues'>mysql Table Locking Issues</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
<li><a href='http://www.masalife.com/archives/426' rel='bookmark' title='Permanent Link: mysql grant 语法'>mysql grant 语法</a></li>
<li><a href='http://www.masalife.com/archives/238' rel='bookmark' title='Permanent Link: discuz7.2勋章模块中的bug'>discuz7.2勋章模块中的bug</a></li>
<li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/1009">Permalink</a> |
<a href="http://www.masalife.com/archives/1009#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/1009&title=恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/mysql" rel="tag">mysql</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/1009/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>discuz 新注册用户无法设置默认编辑器模式，只能以源代码方式发贴</title>
		<link>http://www.masalife.com/archives/1007</link>
		<comments>http://www.masalife.com/archives/1007#comments</comments>
		<pubDate>Sat, 04 Sep 2010 04:41:30 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[discuz]]></category>
		<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=1007</guid>
		<description><![CDATA[discuz 7.2 discuz 新注册用户无法设置默认编辑器模式，只能以源代码方式发贴 后台确定是设置的默认为所见即所得模式。 新注册用户登录之后， 即使在个人设置的“论坛个性化”设置里面， 设置编辑器为所见即所得，也无效。 后来看到include/post.php中: if($prompt) { if($taskon &#038;&#038; ($prompt &#038; 8)) { $prompts['newbietask'] = 1; $disallowfloat = str_replace('task', '', $disallowfloat); $disallowfloat .= '&#124;newthread&#124;reply'; $editormode = 0; } 如果论坛开启了任务，但是新用户并没有完成新手任务， 就设置 editormode为0，表示源代码编辑器。 ps:为2的话，表示使用论坛默认编辑器。 为1表示所见即所得编辑器. Related posts:关于花荣老师 discuz 与 php配置文件中的magic_quotes_gpc wordpress函数之get_posts discuz 短消息群发给版主的功能 限制discuz贴子标题的最小字数 转载请注明出处。 © 花荣 for 花荣老师的博客, 2010. &#124; Permalink &#124; One comment &#124; Add to del.icio.us Post tags: discuz, php IT大腕交流群： 110443867 投稿邮箱： huarong@masalife.com 或者直接联系QQ： 190890101


Related posts:<ol><li><a href='http://www.masalife.com/about' rel='bookmark' title='Permanent Link: 关于花荣老师'>关于花荣老师</a></li>
<li><a href='http://www.masalife.com/archives/194' rel='bookmark' title='Permanent Link: discuz 与 php配置文件中的magic_quotes_gpc'>discuz 与 php配置文件中的magic_quotes_gpc</a></li>
<li><a href='http://www.masalife.com/archives/552' rel='bookmark' title='Permanent Link: wordpress函数之get_posts'>wordpress函数之get_posts</a></li>
<li><a href='http://www.masalife.com/archives/532' rel='bookmark' title='Permanent Link: discuz 短消息群发给版主的功能'>discuz 短消息群发给版主的功能</a></li>
<li><a href='http://www.masalife.com/archives/199' rel='bookmark' title='Permanent Link: 限制discuz贴子标题的最小字数'>限制discuz贴子标题的最小字数</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>discuz 7.2</p>
<p>discuz 新注册用户无法设置默认编辑器模式，只能以源代码方式发贴</p>
<p>后台确定是设置的默认为所见即所得模式。</p>
<p>新注册用户登录之后， 即使在个人设置的“论坛个性化”设置里面，<br />
设置编辑器为所见即所得，也无效。</p>
<p>后来看到include/post.php中:</p>
<pre lang="php">
if($prompt) {<br />
        if($taskon &#038;&#038; ($prompt &#038; 8)) {<br />
                $prompts['newbietask'] = 1;<br />
                $disallowfloat = str_replace('task', '', $disallowfloat);<br />
                $disallowfloat .= '|newthread|reply';<br />
                $editormode = 0;<br />
        }<br />
如果论坛开启了任务，但是新用户并没有完成新手任务， 就设置 editormode为0，表示源代码编辑器。<br />
ps:为2的话，表示使用论坛默认编辑器。 为1表示所见即所得编辑器.</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/about' rel='bookmark' title='Permanent Link: 关于花荣老师'>关于花荣老师</a></li>
<li><a href='http://www.masalife.com/archives/194' rel='bookmark' title='Permanent Link: discuz 与 php配置文件中的magic_quotes_gpc'>discuz 与 php配置文件中的magic_quotes_gpc</a></li>
<li><a href='http://www.masalife.com/archives/552' rel='bookmark' title='Permanent Link: wordpress函数之get_posts'>wordpress函数之get_posts</a></li>
<li><a href='http://www.masalife.com/archives/532' rel='bookmark' title='Permanent Link: discuz 短消息群发给版主的功能'>discuz 短消息群发给版主的功能</a></li>
<li><a href='http://www.masalife.com/archives/199' rel='bookmark' title='Permanent Link: 限制discuz贴子标题的最小字数'>限制discuz贴子标题的最小字数</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/1007">Permalink</a> |
<a href="http://www.masalife.com/archives/1007#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/1007&title=discuz 新注册用户无法设置默认编辑器模式，只能以源代码方式发贴">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/discuz" rel="tag">discuz</a>, <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/1007/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>初步学习php中的Reflection(反射)</title>
		<link>http://www.masalife.com/archives/1005</link>
		<comments>http://www.masalife.com/archives/1005#comments</comments>
		<pubDate>Sat, 04 Sep 2010 02:53:13 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=1005</guid>
		<description><![CDATA[原文地址：http://ctankersley.com/2010/09/03/getting-started-with-reflection/ PHP5添加了一项新的功能：Reflection。 使得程序员可以reverse-engineer class, interface,function,method and extension。 通过PHP代码，就可以得到某object的所有信息，并且可以和它交互。 本文主要讲解ReflectionClass部分。 假设有一个类Person class Person &#123; /** * For the sake of demonstration, we&#34;re setting this private */ private $_allowDynamicAttributes = false; &#160; /** type=primary_autoincrement */ protected $id = 0; &#160; /** type=varchar length=255 null */ protected $name; &#160; /** type=text null */ protected $biography; &#160; public function getId&#40;&#41; &#123; return $this-&#62;id; &#125; public function setId&#40;$v&#41; &#123; $this-&#62;id = $v; &#125; public function getName&#40;&#41; &#123; return $this-&#62;name; &#125; public function setName&#40;$v&#41; &#123; $this-&#62;name = $v; &#125; public function getBiography&#40;&#41; &#123; return $this-&#62;biography; &#125; public function setBiography&#40;$v&#41; &#123; $this-&#62;biography = $v; &#125; &#125; 通过ReflectionClass，我们可以得到Person类的以下信息:&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
<li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
<li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/97' rel='bookmark' title='Permanent Link: 具有缓存功能的php rss类'>具有缓存功能的php rss类</a></li>
<li><a href='http://www.masalife.com/archives/265' rel='bookmark' title='Permanent Link: Jira与confluence的整合'>Jira与confluence的整合</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址：http://ctankersley.com/2010/09/03/getting-started-with-reflection/</p>
<p>PHP5添加了一项新的功能：Reflection。<br />
使得程序员可以reverse-engineer class, interface,function,method and extension。<br />
通过PHP代码，就可以得到某object的所有信息，并且可以和它交互。</p>
<p>本文主要讲解ReflectionClass部分。</p>
<p>假设有一个类Person</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>  
    <span style="color: #009933; font-style: italic;">/** 
     * For the sake of demonstration, we&quot;re setting this private
     */</span> 
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_allowDynamicAttributes</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** type=primary_autoincrement */</span>
    protected <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** type=varchar length=255 null */</span>
    protected <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** type=text null */</span>
    protected <span style="color: #000088;">$biography</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getBiography<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">biography</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setBiography<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">biography</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>通过ReflectionClass，我们可以得到Person类的以下信息:<br />
1 常量 Contants<br />
2 属性 Property Names<br />
3 方法 Method Names<br />
4 静态属性 Static Properties<br />
5 命名空间 Namespace<br />
6 Person类是否为final或者abstract</p>
<p>只要把类名&#8221;Person&#8221;传递给ReflectionClass就可以了</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Person'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>获取属性(Properties)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$properties</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$properties</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// 输出:</span>
<span style="color: #666666; font-style: italic;">// _allowDynamicAttributes</span>
<span style="color: #666666; font-style: italic;">// id</span>
<span style="color: #666666; font-style: italic;">// name</span>
<span style="color: #666666; font-style: italic;">// biography</span></pre></div></div>

<p>默认情况下，ReflectionClass会获取到所有的属性，private 和 protected的也可以。<br />
如果只想获取到private属性，就要额外传个参数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$private_properties</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperties</span><span style="color: #009900;">&#40;</span>ReflectionProperty<span style="color: #339933;">::</span><span style="color: #004000;">IS_PRIVATE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>可用参数列表：<br />
ReflectionProperty::IS_STATIC<br />
ReflectionProperty::IS_PUBLIC<br />
ReflectionProperty::IS_PROTECTED<br />
ReflectionProperty::IS_PRIVATE<br />
如果要同时获取public 和private 属性，就这样写：<br />
ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED<br />
应该不会感觉陌生吧。</p>
<p>继续。<br />
通过$property->getName()可以得到属性名，通过getDocComment可以得到写给property的注释。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$properties</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$docblock</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDocComment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/ type\=([a-z_]*) /'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDocComment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Output:</span>
<span style="color: #666666; font-style: italic;">// primary_autoincrement</span>
<span style="color: #666666; font-style: italic;">// varchar</span>
<span style="color: #666666; font-style: italic;">// text</span></pre></div></div>

<p>有点不可思议了吧。竟然连注释都可以取到。</p>
<p>获取方法(methods):<br />
通过getMethods() 来获取到类的所有methods。返回的是ReflectionMethod对象的数组。不再演示。</p>
<p>最后通过ReflectionMethod来调用类里面的method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Chris&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;biography&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;I am am a PHP developer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; is not a valid property&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;get&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; is missing a getter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;set&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; is missing a setter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Make a new object to interact with</span>
    <span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the getter method and invoke it with the value in our data array</span>
    <span style="color: #000088;">$setter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;set&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$setter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the setter method and invoke it</span>
    <span style="color: #000088;">$setter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;get&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$objValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$setter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Now compare</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$objValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Getter or Setter has modified the data.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Getter and Setter does not modify the data.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>有点意思吧。</p>
<p>不过，感觉起来不太实用？<br />
那么来看这个实用的程序吧：PhpORM_Cli_GenerateSql<br />
http://github.com/dragonmantank/PhpORM/blob/master/PhpORM/Cli/GenerateSql.php ,<br />
作者使用Reflection来构造&#8221;CREATE TABLE&#8221;的sql语句。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
<li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
<li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/97' rel='bookmark' title='Permanent Link: 具有缓存功能的php rss类'>具有缓存功能的php rss类</a></li>
<li><a href='http://www.masalife.com/archives/265' rel='bookmark' title='Permanent Link: Jira与confluence的整合'>Jira与confluence的整合</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/1005">Permalink</a> |
<a href="http://www.masalife.com/archives/1005#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/1005&title=初步学习php中的Reflection(反射)">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/1005/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP垃圾回收机制 之 变量的处理</title>
		<link>http://www.masalife.com/archives/984</link>
		<comments>http://www.masalife.com/archives/984#comments</comments>
		<pubDate>Fri, 03 Sep 2010 07:52:23 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=984</guid>
		<description><![CDATA[原文地址： http://derickrethans.nl/collecting-garbage-phps-take-on-variables.html 关于PHP垃圾回收机制(Garbage Collection . GC) ，原作者写了三篇文章。这是第一篇，主要讲解PHP如何处理变量。 第二篇和第三篇主要讲常用的GC方法，以及GC是如何实现的，以及一些其它的说明和评测。 PHP版本：5.3 进入正题： PHP把变量保存在zval容器里面。容器，container，可以想像成一块存储区域，或者一个盒子。 如上图所示，zval容器保存了此变量的类型type，值value，还有其它二块东西。 其中一个叫做&#8221;is_ref&#8221;, 它是一个bool型的值，占用一个bit，表示该zval容器（也就是这个变量）是否被引用。 php就是使用这个bit来判断变量是个普通变量，或者是个引用（reference）。 说起来，PHP有二种引用： 其一是用户代码中的&#038; 其二是PHP内部实现的引用计数机制（internal reference counting mechanism），用来优化内存使用的。 is_ref是指&#038; zval容器中的另外一部分，是&#8221;refcount&#8221;,引用计数。用来记录有多少个变量指向这个zval容器。 通常吧，这个时候我们只说symbol(符号)，不说variables(变量),其实是同一个意思。 当refcount为1的时候，is_ref必须为0。 当refcount为0的时候，该容器会被删除掉，释放空间出来。 所有的symbol保存在一张symbol table里面（符号表）。 php维护了很多张这样的表，GLOBAL一张，每个函数一张。类的每个方法也有一张。 基本上是按照变量的作用域(scope)来建表的。 当我们用一个constant value（常量）来为变量赋值的时候，zval容器才会被创建出来。 举例： $a = &#34;this is&#34;; 如上图所示；在当前的作用域里面，符号a被创建出来，zval容器也被创建， 类型：string 值： this is is_ref: 默认为0 refcount: 1 表示现在只有一个符号(a)指向这个容器。 如果你安装了Xdebug，可以使用如下的代码： xdebug_debug_zval&#40;&#34;a&#34;&#41;; 可以看到： a: &#40;refcount=1, is_ref=0&#41;=&#34;this is&#34; 貌似看不到变量类型嘛。 再看下面的例子： $a = &#34;this is&#34;; $b = $a; $c = $a; xdebug_debug_zval&#40;&#34;a&#34;&#41;; 可以看到： a: &#40;refcount=3, is_ref=0&#41;=&#34;this is&#34; 如上图所示， refcount 变成3了，表示有3个符号指向这个容器。 看来PHP还是蛮聪明的，它并没有给$b创建一个单独的容器出来,节省了内存。 只有到了必要的时候，才会创建新的容器。 如下： $a = &#34;this is&#34;; $b = $a; $c = $a; $c = 42; 如上图所示， 在$c = 42的时候，一个新的容器被创建出来，原有容器的refcount减一。 如果我们unset一个变量呢？ $a = &#34;this is&#34;;&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/615' rel='bookmark' title='Permanent Link: 计算24点的程序(php版)'>计算24点的程序(php版)</a></li>
<li><a href='http://www.masalife.com/archives/48' rel='bookmark' title='Permanent Link: GOOGLE map 最常用API及示例'>GOOGLE map 最常用API及示例</a></li>
<li><a href='http://www.masalife.com/archives/731' rel='bookmark' title='Permanent Link: 最近在玩的一个喝酒游戏&#8211;猜牙签'>最近在玩的一个喝酒游戏&#8211;猜牙签</a></li>
<li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址： http://derickrethans.nl/collecting-garbage-phps-take-on-variables.html</p>
<p>关于PHP垃圾回收机制(Garbage Collection . GC) ，原作者写了三篇文章。这是第一篇，主要讲解PHP如何处理变量。<br />
第二篇和第三篇主要讲常用的GC方法，以及GC是如何实现的，以及一些其它的说明和评测。</p>
<p>PHP版本：5.3</p>
<p>进入正题：</p>
<p>PHP把变量保存在zval容器里面。容器，container，可以想像成一块存储区域，或者一个盒子。<br />
<a href="http://www.masalife.com/wp-content/uploads/2010/09/11.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/11.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="197" height="91" class="alignnone size-full wp-image-986" /></a><br />
如上图所示，zval容器保存了此变量的类型type，值value，还有其它二块东西。<br />
其中一个叫做&#8221;is_ref&#8221;, 它是一个bool型的值，占用一个bit，表示该zval容器（也就是这个变量）是否被引用。<br />
php就是使用这个bit来判断变量是个普通变量，或者是个引用（reference）。<br />
说起来，PHP有二种引用：<br />
其一是用户代码中的&#038;<br />
其二是PHP内部实现的引用计数机制（internal reference counting mechanism），用来优化内存使用的。<br />
is_ref是指&#038;</p>
<p>zval容器中的另外一部分，是&#8221;refcount&#8221;,引用计数。用来记录有多少个变量指向这个zval容器。<br />
通常吧，这个时候我们只说symbol(符号)，不说variables(变量),其实是同一个意思。<br />
当refcount为1的时候，is_ref必须为0。<br />
当refcount为0的时候，该容器会被删除掉，释放空间出来。</p>
<p>所有的symbol保存在一张symbol table里面（符号表）。<br />
php维护了很多张这样的表，GLOBAL一张，每个函数一张。类的每个方法也有一张。<br />
基本上是按照变量的作用域(scope)来建表的。</p>
<p>当我们用一个constant value（常量）来为变量赋值的时候，zval容器才会被创建出来。<br />
举例：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/2.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/2.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="433" height="93" class="alignnone size-full wp-image-987" /></a><br />
如上图所示；在当前的作用域里面，符号a被创建出来，zval容器也被创建，<br />
类型：string<br />
值： this is<br />
is_ref: 默认为0<br />
refcount: 1 表示现在只有一个符号(a)指向这个容器。</p>
<p>如果你安装了Xdebug，可以使用如下的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">xdebug_debug_zval<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>可以看到：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;this is&quot;</span></pre></div></div>

<p>貌似看不到变量类型嘛。</p>
<p>再看下面的例子：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>可以看到：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;this is&quot;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/3.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/3.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="745" height="317" class="alignnone size-full wp-image-988" /></a><br />
如上图所示， refcount 变成3了，表示有3个符号指向这个容器。<br />
看来PHP还是蛮聪明的，它并没有给$b创建一个单独的容器出来,节省了内存。<br />
只有到了必要的时候，才会创建新的容器。<br />
如下：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/4.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/4.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="676" height="310" class="alignnone size-full wp-image-989" /></a><br />
如上图所示， 在$c = 42的时候，一个新的容器被创建出来，原有容器的refcount减一。</p>
<p>如果我们unset一个变量呢？</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>会显示：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;this is&quot;</span>
a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;this is&quot;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/5.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/5.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="687" height="424" class="alignnone size-full wp-image-990" /></a><br />
如果我们再调用 unset($a)，那么这个zval容器也就一起消失了。</p>
<p>接下来讲引用赋值：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/6.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/6.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="718" height="474" class="alignnone size-full wp-image-991" /></a><br />
如图：符号a b和c都指向同一个容器，而且这个容器的is_ref位为1(因为&#038;出现了)。<br />
表示这是一个引用。refcount为3。<br />
接下来</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/7.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/7.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="656" height="306" class="alignnone size-full wp-image-992" /></a><br />
会把容器的值设置为42。</p>
<p>接下来unset一个变量</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/8.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/8.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="679" height="241" class="alignnone size-full wp-image-993" /></a><br />
容器的refcount减一,c这个符号消失。</p>
<p>再unset($a)呢？<br />
<a href="http://www.masalife.com/wp-content/uploads/2010/09/9.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/9.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="688" height="125" class="alignnone size-full wp-image-994" /></a><br />
refcount继续减一，值为1。同时，	is_ref变回0。引用消失。a符号消失。</p>
<p>您理解了吗？</p>
<p>引用赋值与普通赋值的区别就在于容器的is_ref位为1还是为0。<br />
如果为1，改变任意一个变量的值，只是会更改容器的值。<br />
如果为0，改变任意一个变量的值，都会创建一个新的容器出来。</p>
<p>如果把引用赋值与普通赋值混合起来会怎么样？</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/10.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/10.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="712" height="193" class="alignnone size-full wp-image-995" /></a><br />
如图，二个符号都指向同一个容器。</p>
<p>继续</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/111.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/111.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="749" height="294" class="alignnone size-full wp-image-996" /></a><br />
如图，一个新的容器被分配出来，b和c都指向它，而且is_ref=1。<br />
没什么特殊的。想想也应该是这样。<br />
总不可能abc都指向同一个容器吧。</p>
<p>这样呢？</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;this is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/12.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/12.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="772" height="206" class="alignnone size-full wp-image-997" /></a><br />
如图，二个符号指向同一容器，is_ref=1，因为发生了引用赋值。</p>
<p>继续</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/13.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/13.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="691" height="308" class="alignnone size-full wp-image-998" /></a><br />
如图，一个新的容器被分配出来，c指向了它。 原容器没发生变化。<br />
看到这里，是不是有点乱了。慢慢体会一下吧。</p>
<p>休息一下。</p>
<p>接下来讲PHP如何处理数组和对象。</p>
<p>以数组为例吧，对象也差不多。<br />
每个数组都维护了一张自己的符号表，保存了自己的元素。<br />
看代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;meaning&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;life&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;number&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;a&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>会显示：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;meaning&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;life&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;number&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>很合理，也很合逻辑。<br />
<a href="http://www.masalife.com/wp-content/uploads/2010/09/14.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/14.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="601" height="153" class="alignnone size-full wp-image-999" /></a><br />
如图所示，三个zval容器被创建了出来，&#8221;a&#8221;, &#8220;meaning&#8221;, &#8220;number&#8221;。<br />
数组对refcount的处理，与普通的变量(标量 scalar)是相同的。演示一下看：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;meaning&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;life&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;number&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;life&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;meaning&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;a&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>会显示：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;meaning&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;life&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;number&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">42</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;life&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;life&quot;</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/15.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/15.jpg" alt="PHP垃圾回收机制 之 变量的处理" title="PHP垃圾回收机制 之 变量的处理 " width="603" height="146" class="alignnone size-full wp-image-1000" /></a><br />
如图，life和meaning都指向同一个容器,容器的refcount为2。<br />
和前面所讲的php处理标量的行为是完全一致的。</p>
<p>unset数组的一个元素，与unset一个普通变量的情况也是完全一致的，<br />
删除这个符号，然后refcount减一，如果值为0了，就删除这个容器。不再举例。</p>
<p>最后来看一个奇怪的操作：<br />
我们把数组自身作为它自己的元素，并且使用引用赋值。<br />
好象说得不太明白，看代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;one&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
xdebug_debug_zval<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;a&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果不是引用赋值，就简单多了。不表。</p>
<p>输出为：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">a<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=...</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p><a href="http://www.masalife.com/wp-content/uploads/2010/09/16.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/16.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="546" height="148" class="alignnone size-full wp-image-1001" /></a><br />
需要解释吗？<br />
应该也算是蛮合理的吧。</p>
<p>如果unset a变量，会怎么样？<br />
按照以前的经验，应该是会删除a这个符号，然后refcount减一。如果值为0了，就清理掉容器。<br />
不过从直觉上来讲，$a被unset,那么这个数组也应该消失才对吧？</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>refcount<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> is_ref<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=...</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>虽然在当前的作用域里面，已经没有符号指向这个zval容器，但它并没有被释放出来，<br />
只是因为array[1]还指向着它？<br />
<a href="http://www.masalife.com/wp-content/uploads/2010/09/17.jpg"><img src="http://www.masalife.com/wp-content/uploads/2010/09/17.jpg" alt="PHP垃圾回收机制 之 变量的处理 " title="PHP垃圾回收机制 之 变量的处理 " width="463" height="147" class="alignnone size-full wp-image-1002" /></a><br />
因为没有变量指向这个容器了，所以在php代码中，我们无法对其进行任何操作。<br />
这就出现了内存泄漏(memory leak)。<br />
不过，在脚本执行结束之后，php会把这块区域也释放出来的。</p>
<p>所以，使用引用的时候，一定要小心。</p>
<p>如果你想继续研究，php如何处理function参数，function局部变量,以及更多的引用赋值，请看这个PDF：</p>
<p>http://derickrethans.nl/files/phparch-php-variables-article.pdf</p>
<p>不过要注意，他讲的是php4.3。不知道在5.3的时候，会不会有什么变化。<br />
我就不再翻译了。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/615' rel='bookmark' title='Permanent Link: 计算24点的程序(php版)'>计算24点的程序(php版)</a></li>
<li><a href='http://www.masalife.com/archives/48' rel='bookmark' title='Permanent Link: GOOGLE map 最常用API及示例'>GOOGLE map 最常用API及示例</a></li>
<li><a href='http://www.masalife.com/archives/731' rel='bookmark' title='Permanent Link: 最近在玩的一个喝酒游戏&#8211;猜牙签'>最近在玩的一个喝酒游戏&#8211;猜牙签</a></li>
<li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/984">Permalink</a> |
<a href="http://www.masalife.com/archives/984#comments">2 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/984&title=PHP垃圾回收机制 之 变量的处理">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/984/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>使用php PHPMailer SMTP Gmail来发送邮件</title>
		<link>http://www.masalife.com/archives/980</link>
		<comments>http://www.masalife.com/archives/980#comments</comments>
		<pubDate>Fri, 03 Sep 2010 03:42:12 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=980</guid>
		<description><![CDATA[原文地址：http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/ 为什么要使用Gmail来发送邮件呢？ 首先它是免费的，每天大概可以发送500封邮件。 关于这个数量限制，可以来这里看看：http://mail.google.com/support/bin/answer.py?hl=en&#038;answer=22839 其次您网站所在的服务器IP很有可能已经在垃圾邮件的黑名单里面了。 操作步骤： 1 注册Gmail帐号 2 下载最新版本的PHPMailer http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/ 3 检查服务器防火墙是否允许你连接其它服务器的465端口 4 包含phpmailer的class： require_once&#40;'phpmailer/class.phpmailer.php'&#41;; 5 设置用户名密码: define&#40;'GUSER', 'you@gmail.com'&#41;; // Gmail username define&#40;'GPWD', 'password'&#41;; // Gmail password 6 发送邮件的函数： function smtpmailer&#40;$to, $from, $from_name, $subject, $body&#41; &#123; global $error; $mail = new PHPMailer&#40;&#41;; // create a new object $mail-&#62;IsSMTP&#40;&#41;; // enable SMTP $mail-&#62;SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only $mail-&#62;SMTPAuth = true; // authentication enabled $mail-&#62;SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail $mail-&#62;Host = 'smtp.gmail.com'; $mail-&#62;Port = 465; $mail-&#62;Username = GUSER; $mail-&#62;Password = GPWD; $mail-&#62;SetFrom&#40;$from, $from_name&#41;; $mail-&#62;Subject = $subject; $mail-&#62;Body&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/199' rel='bookmark' title='Permanent Link: 限制discuz贴子标题的最小字数'>限制discuz贴子标题的最小字数</a></li>
<li><a href='http://www.masalife.com/archives/126' rel='bookmark' title='Permanent Link: discuz6升级到7之后，新注册用户无法登录xspace的解决办法'>discuz6升级到7之后，新注册用户无法登录xspace的解决办法</a></li>
<li><a href='http://www.masalife.com/archives/499' rel='bookmark' title='Permanent Link: google webfonts 网页字体在线服务启动'>google webfonts 网页字体在线服务启动</a></li>
<li><a href='http://www.masalife.com/archives/961' rel='bookmark' title='Permanent Link: 使用php来压缩JavaScript代码'>使用php来压缩JavaScript代码</a></li>
<li><a href='http://www.masalife.com/archives/412' rel='bookmark' title='Permanent Link: discuz中添加新的论坛动态'>discuz中添加新的论坛动态</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址：http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/</p>
<p>为什么要使用Gmail来发送邮件呢？<br />
首先它是免费的，每天大概可以发送500封邮件。<br />
关于这个数量限制，可以来这里看看：http://mail.google.com/support/bin/answer.py?hl=en&#038;answer=22839<br />
其次您网站所在的服务器IP很有可能已经在垃圾邮件的黑名单里面了。</p>
<p>操作步骤：<br />
1 注册Gmail帐号<br />
2 下载最新版本的PHPMailer http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/<br />
3 检查服务器防火墙是否允许你连接其它服务器的465端口<br />
4 包含phpmailer的class：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'phpmailer/class.phpmailer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>5 设置用户名密码:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GUSER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'you@gmail.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Gmail username</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GPWD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Gmail password</span></pre></div></div>

<p>6 发送邮件的函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> smtpmailer<span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// create a new object</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">IsSMTP</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// enable SMTP</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SMTPDebug</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// debugging: 1 = errors and messages, 2 = messages only</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SMTPAuth</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// authentication enabled</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SMTPSecure</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ssl'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// secure transfer enabled REQUIRED for Gmail</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'smtp.gmail.com'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">465</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Username</span> <span style="color: #339933;">=</span> GUSER<span style="color: #339933;">;</span>  
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Password</span> <span style="color: #339933;">=</span> GPWD<span style="color: #339933;">;</span>           
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$body</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Mail error: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span> 
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Message sent!'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>另外为了保证邮件发送的成功率，最好是设置一下您域名的SPF记录。<br />
各大网站都设置了呢。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">163<span style="color: #339933;">.</span>com<span style="color: #339933;">.</span>                <span style="color: #cc66cc;">14836</span>   IN      TXT     <span style="color: #0000ff;">&quot;v=spf1 include:spf.163.com -all&quot;</span>
qq<span style="color: #339933;">.</span>com<span style="color: #339933;">.</span>                 <span style="color: #cc66cc;">2614</span>    IN      TXT     <span style="color: #0000ff;">&quot;v=spf1 ip4:119.147.10.0/23 ip4:119.147.18.0/24 ip4:222.202.96.0/24 ip4:64.71.138.0/25 ip4:58.251.149.0/24 ip4:119.147.6.0/24 ip4:119.147.8.0/24 ip4:112.90.137.0/22 ip4:119.147.14.0/24 ip4:183.60.2.0/24 ip4:113.108.77.0/24 ip4:183.62.126.0/24 ~all&quot;</span>
sina<span style="color: #339933;">.</span>com<span style="color: #339933;">.</span>               <span style="color: #cc66cc;">60</span>      IN      TXT     <span style="color: #0000ff;">&quot;v=spf1 include:spf.sinamail.sina.com.cn -all&quot;</span></pre></div></div>

<p>关于SPF可以在这里看一下说明：</p>
<p>http://www.google.com/support/a/bin/answer.py?hl=en&#038;answer=33786</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/199' rel='bookmark' title='Permanent Link: 限制discuz贴子标题的最小字数'>限制discuz贴子标题的最小字数</a></li>
<li><a href='http://www.masalife.com/archives/126' rel='bookmark' title='Permanent Link: discuz6升级到7之后，新注册用户无法登录xspace的解决办法'>discuz6升级到7之后，新注册用户无法登录xspace的解决办法</a></li>
<li><a href='http://www.masalife.com/archives/499' rel='bookmark' title='Permanent Link: google webfonts 网页字体在线服务启动'>google webfonts 网页字体在线服务启动</a></li>
<li><a href='http://www.masalife.com/archives/961' rel='bookmark' title='Permanent Link: 使用php来压缩JavaScript代码'>使用php来压缩JavaScript代码</a></li>
<li><a href='http://www.masalife.com/archives/412' rel='bookmark' title='Permanent Link: discuz中添加新的论坛动态'>discuz中添加新的论坛动态</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/980">Permalink</a> |
<a href="http://www.masalife.com/archives/980#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/980&title=使用php PHPMailer SMTP Gmail来发送邮件">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/google" rel="tag">google</a>, <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/980/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mysql存储过程&#8211;自动删除长期不活动的连接</title>
		<link>http://www.masalife.com/archives/974</link>
		<comments>http://www.masalife.com/archives/974#comments</comments>
		<pubDate>Fri, 03 Sep 2010 02:57:35 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=974</guid>
		<description><![CDATA[原文地址： http://feedproxy.google.com/~r/blogspot/yyLW/~3/i0SICZyo1Wk/mysql-kill-sleeping-connections.html 要翻墙。 mysqld的wait_timeout通常都比较大，有的连接也不会自己断开，就在那里Sleep着，占用一个连接数。 人工去kill的话，显然比较累，那么就写一个存储过程吧，让它自己去执行。 使用root登录mysql之后，运行以下代码： USE test; &#160; DELIMITER $$ DROP PROCEDURE IF EXISTS `uKillSleepingSessions`$$ &#160; &#160; CREATE PROCEDURE `uKillSleepingSessions`&#40;&#41; COMMENT 'This routne is used to kill idle sessions' READS SQL DATA &#160; &#160; BEGIN &#160; &#160; DECLARE no_more_rows BOOLEAN; DECLARE loop_cntr INT DEFAULT 0; DECLARE num_rows INT DEFAULT 0; DECLARE uID bigint&#40;4&#41;; &#160; DECLARE my_cur CURSOR FOR SELECT ID FROM information_schema.PROCESSLIST PL WHERE PL.COMMAND='Sleep' AND PL.TIME &#62; 180; &#160; &#160; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE; &#160; &#160; OPEN my_cur; select FOUND_ROWS&#40;&#41; into num_rows; &#160; the_loop: LOOP &#160; &#160; FETCH my_cur INTO uID; &#160;&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/426' rel='bookmark' title='Permanent Link: mysql grant 语法'>mysql grant 语法</a></li>
<li><a href='http://www.masalife.com/archives/173' rel='bookmark' title='Permanent Link: 使用Mysql 的镜像功能进行数据同步'>使用Mysql 的镜像功能进行数据同步</a></li>
<li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/401' rel='bookmark' title='Permanent Link: drupal_goto 函数详解'>drupal_goto 函数详解</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址： http://feedproxy.google.com/~r/blogspot/yyLW/~3/i0SICZyo1Wk/mysql-kill-sleeping-connections.html<br />
要翻墙。</p>
<p>mysqld的wait_timeout通常都比较大，有的连接也不会自己断开，就在那里Sleep着，占用一个连接数。<br />
人工去kill的话，显然比较累，那么就写一个存储过程吧，让它自己去执行。</p>
<p>使用root登录mysql之后，运行以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">USE test<span style="color: #339933;">;</span>
&nbsp;
DELIMITER $$ 
DROP PROCEDURE <span style="color: #b1b100;">IF</span> EXISTS `uKillSleepingSessions`$$
&nbsp;
&nbsp;
CREATE PROCEDURE `uKillSleepingSessions`<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
COMMENT <span style="color: #0000ff;">'This routne is used to kill idle sessions'</span>
READS SQL DATA 
&nbsp;
&nbsp;
BEGIN 
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">DECLARE</span> no_more_rows BOOLEAN<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">DECLARE</span> loop_cntr INT <span style="color: #b1b100;">DEFAULT</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">DECLARE</span> num_rows INT <span style="color: #b1b100;">DEFAULT</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">DECLARE</span> uID bigint<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">DECLARE</span> my_cur CURSOR <span style="color: #b1b100;">FOR</span>
SELECT ID 
  FROM information_schema<span style="color: #339933;">.</span>PROCESSLIST PL
WHERE PL<span style="color: #339933;">.</span>COMMAND<span style="color: #339933;">=</span><span style="color: #0000ff;">'Sleep'</span> AND PL<span style="color: #339933;">.</span><span style="color: #990000;">TIME</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">180</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">DECLARE</span> <span style="color: #b1b100;">CONTINUE</span> HANDLER <span style="color: #b1b100;">FOR</span> NOT FOUND
SET no_more_rows <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
OPEN my_cur<span style="color: #339933;">;</span>
select FOUND_ROWS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> into num_rows<span style="color: #339933;">;</span>
&nbsp;
the_loop<span style="color: #339933;">:</span> LOOP
&nbsp;
&nbsp;
FETCH my_cur
INTO uID<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">IF</span> no_more_rows THEN
    CLOSE my_cur<span style="color: #339933;">;</span>
    LEAVE the_loop<span style="color: #339933;">;</span>
<span style="color: #990000;">END</span> <span style="color: #b1b100;">IF</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
SET <span style="color: #339933;">@</span>tmp_sql<span style="color: #339933;">=</span> CONCAT<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;KILL &quot;</span><span style="color: #339933;">,</span>uID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
PREPARE s1 FROM <span style="color: #339933;">@</span>tmp_sql<span style="color: #339933;">;</span> 
EXECUTE s1<span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
DEALLOCATE PREPARE s1<span style="color: #339933;">;</span> 
SET loop_cntr <span style="color: #339933;">=</span> loop_cntr <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #990000;">END</span> LOOP the_loop<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #990000;">END</span>$$
DELIMITER <span style="color: #339933;">;</span></pre></div></div>

<p>调用方法：<br />
root登录到mysql之后，运行：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">call test<span style="color: #339933;">.</span>uKillSleepingSessions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>就会自动kill sleep超过180秒的连接了。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">mysql<span style="color: #339933;">&gt;</span> show processlist<span style="color: #339933;">;</span>
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #339933;">|</span> Id       <span style="color: #339933;">|</span> User <span style="color: #339933;">|</span> Host      <span style="color: #339933;">|</span> db   <span style="color: #339933;">|</span> Command <span style="color: #339933;">|</span> <span style="color: #990000;">Time</span> <span style="color: #339933;">|</span> State <span style="color: #339933;">|</span> Info             <span style="color: #339933;">|</span>
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #339933;">|</span> <span style="color: #cc66cc;">34035650</span> <span style="color: #339933;">|</span> root <span style="color: #339933;">|</span> localhost <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #339933;">|</span> <span style="color: #990000;">Sleep</span>   <span style="color: #339933;">|</span>  <span style="color: #cc66cc;">251</span> <span style="color: #339933;">|</span>       <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">NULL</span>             <span style="color: #339933;">|</span> 
<span style="color: #339933;">|</span> <span style="color: #cc66cc;">34036126</span> <span style="color: #339933;">|</span> root <span style="color: #339933;">|</span> localhost <span style="color: #339933;">|</span> test <span style="color: #339933;">|</span> Query   <span style="color: #339933;">|</span>    <span style="color: #cc66cc;">0</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">NULL</span>  <span style="color: #339933;">|</span> show processlist <span style="color: #339933;">|</span> 
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #cc66cc;">2</span> rows in set <span style="color: #009900;">&#40;</span><span style="color:#800080;">0.00</span> sec<span style="color: #009900;">&#41;</span>
&nbsp;
mysql<span style="color: #339933;">&gt;</span> call test<span style="color: #339933;">.</span>uKillSleepingSessions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Query OK<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span> rows affected <span style="color: #009900;">&#40;</span><span style="color:#800080;">0.01</span> sec<span style="color: #009900;">&#41;</span>
&nbsp;
mysql<span style="color: #339933;">&gt;</span> show processlist<span style="color: #339933;">;</span>            
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #339933;">|</span> Id       <span style="color: #339933;">|</span> User <span style="color: #339933;">|</span> Host      <span style="color: #339933;">|</span> db   <span style="color: #339933;">|</span> Command <span style="color: #339933;">|</span> <span style="color: #990000;">Time</span> <span style="color: #339933;">|</span> State <span style="color: #339933;">|</span> Info             <span style="color: #339933;">|</span>
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #339933;">|</span> <span style="color: #cc66cc;">34036126</span> <span style="color: #339933;">|</span> root <span style="color: #339933;">|</span> localhost <span style="color: #339933;">|</span> test <span style="color: #339933;">|</span> Query   <span style="color: #339933;">|</span>    <span style="color: #cc66cc;">0</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">NULL</span>  <span style="color: #339933;">|</span> show processlist <span style="color: #339933;">|</span> 
<span style="color: #339933;">+----------+------+-----------+------+---------+------+-------+------------------+</span>
<span style="color: #cc66cc;">1</span> row in set <span style="color: #009900;">&#40;</span><span style="color:#800080;">0.00</span> sec<span style="color: #009900;">&#41;</span></pre></div></div>

<p>可以写个cron</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">*/</span><span style="color: #cc66cc;">10</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span>  <span style="color: #339933;">/</span>usr<span style="color: #339933;">/</span>bin<span style="color: #339933;">/</span><span style="color: #990000;">mysql</span> <span style="color: #339933;">-</span>uroot <span style="color: #339933;">-</span>pxxxx <span style="color: #339933;">-</span>e <span style="color: #0000ff;">&quot;call test.uKillSleepingSessions()&quot;</span></pre></div></div>



<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/426' rel='bookmark' title='Permanent Link: mysql grant 语法'>mysql grant 语法</a></li>
<li><a href='http://www.masalife.com/archives/173' rel='bookmark' title='Permanent Link: 使用Mysql 的镜像功能进行数据同步'>使用Mysql 的镜像功能进行数据同步</a></li>
<li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/401' rel='bookmark' title='Permanent Link: drupal_goto 函数详解'>drupal_goto 函数详解</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/974">Permalink</a> |
<a href="http://www.masalife.com/archives/974#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/974&title=mysql存储过程&#8211;自动删除长期不活动的连接">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/mysql" rel="tag">mysql</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/974/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBA的新工具&#8211;笔和笔记本</title>
		<link>http://www.masalife.com/archives/971</link>
		<comments>http://www.masalife.com/archives/971#comments</comments>
		<pubDate>Fri, 03 Sep 2010 02:28:31 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[dba]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=971</guid>
		<description><![CDATA[原文地址： http://opensourcedba.wordpress.com/2010/09/01/pen-and-paper-as-a-dba-tool/ PS:这里说的笔记本是指几块钱的那种。不是laptop。 &#8220;嗨，亲爱的DBA。您还记得四五个月之前，修改过192.168.1.111上面mysql的配置吗？修改之后好象没什么效果，您能尽快把配置改回原来的样子吗？&#8221; 这简直是DBA的恶梦。我们怎么可能记得当时改过什么？ 要知道我有数十台的mysql服务器要维护的。 最简单的办法： 准备一个笔记本，还有一支笔。时刻带在你身边。 无论何时你要对数据库进行操作，请记录下来您所做的改动，为什么要这样改，日期。 开始的时候，可能会觉得无聊，请坚持下来，它一定会帮你的大忙。 并且，一定要写得比较清楚。如果您自己都认不出来，那就完了。 有人可能会问了，我把这些改动记录到我的电脑里面不行吗？ 原因如下： 1 笔记本重量轻，更容易携带。 2 不需要用电 3 展示您的一手好字。 然后，请把所有的my.cnf打印出来，放到笔记本里面。 Related posts:mysql存储过程&#8211;自动删除长期不活动的连接 快速关闭使用InnoDB的mysqld服务 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库 mysql 时间函数 jira与mysql的整合 转载请注明出处。 © 花荣 for 花荣老师的博客, 2010. &#124; Permalink &#124; No comment &#124; Add to del.icio.us Post tags: dba, mysql IT大腕交流群： 110443867 投稿邮箱： huarong@masalife.com 或者直接联系QQ： 190890101


Related posts:<ol><li><a href='http://www.masalife.com/archives/974' rel='bookmark' title='Permanent Link: mysql存储过程&#8211;自动删除长期不活动的连接'>mysql存储过程&#8211;自动删除长期不活动的连接</a></li>
<li><a href='http://www.masalife.com/archives/965' rel='bookmark' title='Permanent Link: 快速关闭使用InnoDB的mysqld服务'>快速关闭使用InnoDB的mysqld服务</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
<li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址： http://opensourcedba.wordpress.com/2010/09/01/pen-and-paper-as-a-dba-tool/<br />
<em>PS:这里说的笔记本是指几块钱的那种。不是laptop。<br />
</em><br />
&#8220;嗨，亲爱的DBA。您还记得四五个月之前，修改过192.168.1.111上面mysql的配置吗？修改之后好象没什么效果，您能尽快把配置改回原来的样子吗？&#8221;</p>
<p>这简直是DBA的恶梦。我们怎么可能记得当时改过什么？<br />
要知道我有数十台的mysql服务器要维护的。</p>
<p>最简单的办法：<br />
准备一个笔记本，还有一支笔。时刻带在你身边。<br />
无论何时你要对数据库进行操作，请记录下来您所做的改动，为什么要这样改，日期。<br />
开始的时候，可能会觉得无聊，请坚持下来，它一定会帮你的大忙。</p>
<p>并且，一定要写得比较清楚。如果您自己都认不出来，那就完了。</p>
<p>有人可能会问了，我把这些改动记录到我的电脑里面不行吗？<br />
原因如下：<br />
1 笔记本重量轻，更容易携带。<br />
2 不需要用电<br />
3 展示您的一手好字。</p>
<p>然后，请把所有的my.cnf打印出来，放到笔记本里面。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/974' rel='bookmark' title='Permanent Link: mysql存储过程&#8211;自动删除长期不活动的连接'>mysql存储过程&#8211;自动删除长期不活动的连接</a></li>
<li><a href='http://www.masalife.com/archives/965' rel='bookmark' title='Permanent Link: 快速关闭使用InnoDB的mysqld服务'>快速关闭使用InnoDB的mysqld服务</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
<li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/971">Permalink</a> |
<a href="http://www.masalife.com/archives/971#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/971&title=DBA的新工具&#8211;笔和笔记本">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/dba" rel="tag">dba</a>, <a href="http://www.masalife.com/archives/tag/mysql" rel="tag">mysql</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/971/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>快速关闭使用InnoDB的mysqld服务</title>
		<link>http://www.masalife.com/archives/965</link>
		<comments>http://www.masalife.com/archives/965#comments</comments>
		<pubDate>Fri, 03 Sep 2010 01:37:55 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[innodb]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=965</guid>
		<description><![CDATA[原文地址： http://www.mysqlperformanceblog.com/2010/09/02/how-long-innodb-shutdown-may-take/ 以及 http://www.mysqlperformanceblog.com/2009/04/15/how-to-decrease-innodb-shutdown-times/ 有时候，关闭使用InnoDB的mysqld服务，速度相当慢， 数分钟之久，一度让我以为会有什么错误发生。 这是因为mysqld要把内存buffer pool里面的dirty pages 写回到硬盘。 innodb_max_dirty_pages_pct 此参数控制buffer pool里面最多有百分之多少的pages可以是dirty page。 默认值是90。也就是说buffer pool的90%都有可能是dirty pages。 写入硬盘所用的时间还跟另外二个因素有关： 1 硬盘。 普通硬盘每秒可以写入200次。牛X一些的可以每秒写入数千次。写回是多线程的，如果您的数据分布在多块硬盘上，那么速度还是会快一些的。 2 dirty pages是否连续。 如果连续，mysqld可能每次会写入1MB的数据。如果不连续的话，那就只能一页一页地写了。通常每页大小是16K或者8K。 如果我们只有一块每秒200次写入的硬盘，48G的buffer pool, 90%是dirty pages,而且完全是随机不连续的page，那么需要13500秒才能完全写入。 如果在写回的过程中，服务器断电，或者您kill -9 mysqld了，也不用担心数据出错。 下次mysqld启动的时候，会根据日志进行数据恢复。可能需要更长的时间。 一个加速的办法是：pre-flush 。 提前写回dirty pages. 操作方法： mysql&#62; set global innodb_max_dirty_pages_pct = 0; $ mysqladmin ext -i10 -uroot -p &#124; grep dirty 可以查看到目前buffer pool中有多少个dirty page,每10秒刷新一次。 当这个值比较小的时候，您就可以关闭mysqld了。 关闭速度一定非常的快。 Related posts:jira与mysql的整合 mysql Table Locking Issues mysql存储过程&#8211;自动删除长期不活动的连接 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库 mysql 时间函数


Related posts:<ol><li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
<li><a href='http://www.masalife.com/archives/184' rel='bookmark' title='Permanent Link: mysql Table Locking Issues'>mysql Table Locking Issues</a></li>
<li><a href='http://www.masalife.com/archives/974' rel='bookmark' title='Permanent Link: mysql存储过程&#8211;自动删除长期不活动的连接'>mysql存储过程&#8211;自动删除长期不活动的连接</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址： http://www.mysqlperformanceblog.com/2010/09/02/how-long-innodb-shutdown-may-take/<br />
以及</p>
<p>http://www.mysqlperformanceblog.com/2009/04/15/how-to-decrease-innodb-shutdown-times/</p>
<p>有时候，关闭使用InnoDB的mysqld服务，速度相当慢，<br />
数分钟之久，一度让我以为会有什么错误发生。<br />
这是因为mysqld要把内存buffer pool里面的dirty pages 写回到硬盘。</p>
<p>innodb_max_dirty_pages_pct 此参数控制buffer pool里面最多有百分之多少的pages可以是dirty page。<br />
默认值是90。也就是说buffer pool的90%都有可能是dirty pages。</p>
<p>写入硬盘所用的时间还跟另外二个因素有关：<br />
1 硬盘。 普通硬盘每秒可以写入200次。牛X一些的可以每秒写入数千次。写回是多线程的，如果您的数据分布在多块硬盘上，那么速度还是会快一些的。<br />
2 dirty pages是否连续。 如果连续，mysqld可能每次会写入1MB的数据。如果不连续的话，那就只能一页一页地写了。通常每页大小是16K或者8K。</p>
<p>如果我们只有一块每秒200次写入的硬盘，48G的buffer pool, 90%是dirty pages,而且完全是随机不连续的page，那么需要13500秒才能完全写入。</p>
<p>如果在写回的过程中，服务器断电，或者您kill -9 mysqld了，也不用担心数据出错。<br />
下次mysqld启动的时候，会根据日志进行数据恢复。可能需要更长的时间。</p>
<p>一个加速的办法是：pre-flush 。 提前写回dirty pages.<br />
操作方法：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">mysql<span style="color: #339933;">&gt;</span> set <span style="color: #000000; font-weight: bold;">global</span> innodb_max_dirty_pages_pct <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">$ mysqladmin ext <span style="color: #339933;">-</span>i10 <span style="color: #339933;">-</span>uroot <span style="color: #339933;">-</span>p <span style="color: #339933;">|</span> grep dirty</pre></div></div>

<p>可以查看到目前buffer pool中有多少个dirty page,每10秒刷新一次。<br />
当这个值比较小的时候，您就可以关闭mysqld了。<br />
关闭速度一定非常的快。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/255' rel='bookmark' title='Permanent Link: jira与mysql的整合'>jira与mysql的整合</a></li>
<li><a href='http://www.masalife.com/archives/184' rel='bookmark' title='Permanent Link: mysql Table Locking Issues'>mysql Table Locking Issues</a></li>
<li><a href='http://www.masalife.com/archives/974' rel='bookmark' title='Permanent Link: mysql存储过程&#8211;自动删除长期不活动的连接'>mysql存储过程&#8211;自动删除长期不活动的连接</a></li>
<li><a href='http://www.masalife.com/archives/1009' rel='bookmark' title='Permanent Link: 恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库'>恢复mysql数据的另外一种方法&#8211;mysql replication 主从数据库</a></li>
<li><a href='http://www.masalife.com/archives/360' rel='bookmark' title='Permanent Link: mysql 时间函数'>mysql 时间函数</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/965">Permalink</a> |
<a href="http://www.masalife.com/archives/965#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/965&title=快速关闭使用InnoDB的mysqld服务">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/innodb" rel="tag">innodb</a>, <a href="http://www.masalife.com/archives/tag/mysql" rel="tag">mysql</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/965/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用php来压缩JavaScript代码</title>
		<link>http://www.masalife.com/archives/961</link>
		<comments>http://www.masalife.com/archives/961#comments</comments>
		<pubDate>Thu, 02 Sep 2010 08:47:56 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[花荣在写JS代码]]></category>
		<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=961</guid>
		<description><![CDATA[原文地址： http://www.sitepoint.com/blogs/2010/08/30/compress-javascript-closure-compiler-rest-api/ 以及： http://www.sitepoint.com/blogs/2010/08/31/compress-javascript-with-php/ 随着ajax的盛行，我们网站要调用的JavaScript代码越来越多， 虽然客户端和服务端的带宽都在不断地提升, PS:最近杭州电信有个说法是“光城市”，光纤覆盖的城市 但我们还是应该优化一下JavaScript代码。 一般来说，有三种优化的方式： １　把 &#60;script&#62; 标签放到 &#60;/body&#62; 之后，这样网站内容可以尽快显示出来。 2 把多个JS文件合并成一个。有效减少HTTP请求次数。 3 压缩JS文件，去掉其中的空格，空行，缩短变量名，以及其它的优化方式。 虽然网络上已经有N多的压缩软件，或者在线压缩程序，可以帮助我们压缩JS， 我经常使用的是GOOGLE提供的Closure Compiler在线压缩， 网址是： http://closure-compiler.appspot.com/home 但这通常是一个手工的过程。 对于大多数开发者来说， 在开发环境中，要修改未压缩的JS文件， 开发完毕之后，手工对其进行压缩， 再发布到生产环境中。 有没有自动的办法呢？ GOOGLE的Closure Compiler终于提供REST API了，可以用PHP来调用它了。 我们想做到： 1 压缩之后，生成错误报告 2 发布未压缩版本的JS文件到测试和生产环境中 3 测试人员和普通用户要访问到压缩之后的JS代码。 4 版本控制机制 5 自动进行压缩 假设我们的网站最后调用三个JS文件 &#60;script src=&#34;script/file1.js&#34;&#62;&#60;/script&#62; &#60;script src=&#34;script/file2.js&#34;&#62;&#60;/script&#62; &#60;script src=&#34;script/file3.js&#34;&#62;&#60;/script&#62; 首先我们需要把这段代码替换成： &#60;script src=&#34;script/?file1&#38;file2&#38;file3&#34;&#62;&#60;/script&#62; 然后在script目录中创建一个index.php &#60;?php // fetch JavaScript files to compress // 得到JS文件名 $jsfiles = array_keys&#40;$_GET&#41;; $js = ''; // code to compress $jscomp = ''; // compressed JS $err = ''; // error string // fetch JavaScript files // 读取所有的JS文件内容，并且合并到一起。 // 如果出错，就记录下文件名。 for &#40;$i = 0, $j = count&#40;$jsfiles&#41;;&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/385' rel='bookmark' title='Permanent Link: chrome浏览器下discuz  ajaxget函数中的问题'>chrome浏览器下discuz  ajaxget函数中的问题</a></li>
<li><a href='http://www.masalife.com/archives/419' rel='bookmark' title='Permanent Link: ecmall 2.2 final GBK版选择日期的bug'>ecmall 2.2 final GBK版选择日期的bug</a></li>
<li><a href='http://www.masalife.com/archives/984' rel='bookmark' title='Permanent Link: PHP垃圾回收机制 之 变量的处理'>PHP垃圾回收机制 之 变量的处理</a></li>
<li><a href='http://www.masalife.com/archives/90' rel='bookmark' title='Permanent Link: 使用JQuery取得html中所有display为none的元素'>使用JQuery取得html中所有display为none的元素</a></li>
<li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文地址：</p>
<p>http://www.sitepoint.com/blogs/2010/08/30/compress-javascript-closure-compiler-rest-api/</p>
<p>以及：</p>
<p>http://www.sitepoint.com/blogs/2010/08/31/compress-javascript-with-php/</p>
<p>随着ajax的盛行，我们网站要调用的JavaScript代码越来越多，<br />
虽然客户端和服务端的带宽都在不断地提升,<br />
<em>PS:最近杭州电信有个说法是“光城市”，光纤覆盖的城市<br />
</em>但我们还是应该优化一下JavaScript代码。<br />
一般来说，有三种优化的方式：<br />
１　把</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script&gt;</pre></div></div>

<p>标签放到</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;/body&gt;</pre></div></div>

<p>之后，这样网站内容可以尽快显示出来。<br />
2   把多个JS文件合并成一个。有效减少HTTP请求次数。<br />
3   压缩JS文件，去掉其中的空格，空行，缩短变量名，以及其它的优化方式。</p>
<p>虽然网络上已经有N多的压缩软件，或者在线压缩程序，可以帮助我们压缩JS，<br />
我经常使用的是GOOGLE提供的Closure Compiler在线压缩，<br />
网址是： http://closure-compiler.appspot.com/home<br />
但这通常是一个手工的过程。<br />
对于大多数开发者来说，<br />
在开发环境中，要修改未压缩的JS文件，<br />
开发完毕之后，手工对其进行压缩，<br />
再发布到生产环境中。</p>
<p>有没有自动的办法呢？<br />
GOOGLE的Closure Compiler终于提供REST API了，可以用PHP来调用它了。<br />
我们想做到：<br />
1 压缩之后，生成错误报告<br />
2 发布未压缩版本的JS文件到测试和生产环境中<br />
3 测试人员和普通用户要访问到压缩之后的JS代码。<br />
4 版本控制机制<br />
5 自动进行压缩</p>
<p>假设我们的网站最后调用三个JS文件</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script/file1.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>  
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script/file2.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>  
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script/file3.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>首先我们需要把这段代码替换成：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script/?file1&amp;file2&amp;file3&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>然后在script目录中创建一个index.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>  
<span style="color: #666666; font-style: italic;">// fetch JavaScript files to compress  </span>
<span style="color: #666666; font-style: italic;">// 得到JS文件名</span>
<span style="color: #000088;">$jsfiles</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #000088;">$js</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">// code to compress  </span>
<span style="color: #000088;">$jscomp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// compressed JS  </span>
<span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">// error string</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// fetch JavaScript files  </span>
<span style="color: #666666; font-style: italic;">// 读取所有的JS文件内容，并且合并到一起。</span>
<span style="color: #666666; font-style: italic;">// 如果出错，就记录下文件名。</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsfiles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>   
<span style="color: #009900;">&#123;</span>  
  <span style="color: #000088;">$fn</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$jsfiles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.js'</span><span style="color: #339933;">;</span>  
  <span style="color: #000088;">$jscode</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jscode</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000088;">$js</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$jscode</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>  
  <span style="color: #009900;">&#125;</span>  
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000088;">$err</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$fn</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'; '</span><span style="color: #339933;">;</span>  
  <span style="color: #009900;">&#125;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$err</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #666666; font-style: italic;">// error: missing files  </span>
  <span style="color: #666666; font-style: italic;">// 如果出错的话，就返回如下的JS。提醒开发者。</span>
  <span style="color: #000088;">$jscomp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;alert('The following JavaScript files   
could not be read:<span style="color: #000099; font-weight: bold;">\\</span>n<span style="color: #006699; font-weight: bold;">$err</span>');&quot;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>如果没有出错：<br />
$apiArgs 数组存放的是Closure Compiler API Options。<br />
更多options请见： http://code.google.com/closure/compiler/docs/api-ref.html</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #666666; font-style: italic;">// REST API arguments  </span>
  <span style="color: #000088;">$apiArgs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
    <span style="color: #0000ff;">'compilation_level'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ADVANCED_OPTIMIZATIONS'</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">//优化度</span>
    <span style="color: #0000ff;">'output_format'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span>  		     
    <span style="color: #0000ff;">'output_info'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'compiled_code'</span>  
   <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #666666; font-style: italic;">//$js 就是所有的JS代码的合体</span>
   <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'js_code='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #666666; font-style: italic;">//把它们拼到一起,一定很长。</span>
   <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$apiArgs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
     <span style="color: #000088;">$args</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'='</span><span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><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: #009900;">&#125;</span></pre></div></div>

<p>接下来就可以去调用REST API了，还是继续用CURL吧：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// API call using cURL  </span>
  <span style="color: #000088;">$call</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  <span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$call</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
    CURLOPT_URL <span style="color: #339933;">=&gt;</span>   
<span style="color: #0000ff;">'http://closure-compiler.appspot.com/compile'</span><span style="color: #339933;">,</span>  
    CURLOPT_POST <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>  
    CURLOPT_POSTFIELDS <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span>  
    CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>  
    CURLOPT_HEADER <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>  
    CURLOPT_FOLLOWLOCATION <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>  
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
	<span style="color: #666666; font-style: italic;">//$jscomp 就是返回的压缩之后的JS代码</span>
  <span style="color: #000088;">$jscomp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$call</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$call</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>最后输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//end of if-else </span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output content   </span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: text/javascript'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$jscomp</span><span style="color: #339933;">;</span>   
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>完整代码下载地址：http://blogs.sitepointstatic.com/examples/tech/jscompress/php-js-compress.zip</p>
<p>但是这还没有完。<br />
如果仅仅是这样， 用户每次访问的时候，都会去调用一下这个REST API，速度可想而知了。<br />
后续的完善工作：<br />
1 错误处理： 需要检查API调用的返回值，以及压缩过程中，Closure Compiler生成的错误报告。<br />
2 服务端缓存： 压缩一次之后，写入缓存文件。<br />
3 客户端缓存： 设置HTTP Expire headers。 这样客户端直接缓存住了。<br />
4 js的URL后面加上版本号：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script/?file1&amp;file2&amp;file3=1.1.11&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>以便在JS更新之后，可以立即更新客户端缓存。</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/385' rel='bookmark' title='Permanent Link: chrome浏览器下discuz  ajaxget函数中的问题'>chrome浏览器下discuz  ajaxget函数中的问题</a></li>
<li><a href='http://www.masalife.com/archives/419' rel='bookmark' title='Permanent Link: ecmall 2.2 final GBK版选择日期的bug'>ecmall 2.2 final GBK版选择日期的bug</a></li>
<li><a href='http://www.masalife.com/archives/984' rel='bookmark' title='Permanent Link: PHP垃圾回收机制 之 变量的处理'>PHP垃圾回收机制 之 变量的处理</a></li>
<li><a href='http://www.masalife.com/archives/90' rel='bookmark' title='Permanent Link: 使用JQuery取得html中所有display为none的元素'>使用JQuery取得html中所有display为none的元素</a></li>
<li><a href='http://www.masalife.com/archives/955' rel='bookmark' title='Permanent Link: PHP和couchDB'>PHP和couchDB</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/961">Permalink</a> |
<a href="http://www.masalife.com/archives/961#comments">3 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/961&title=使用php来压缩JavaScript代码">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/google" rel="tag">google</a>, <a href="http://www.masalife.com/archives/tag/javascript" rel="tag">JavaScript</a>, <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a>, <a href="http://www.masalife.com/archives/tag/rest" rel="tag">rest</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/961/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP和couchDB</title>
		<link>http://www.masalife.com/archives/955</link>
		<comments>http://www.masalife.com/archives/955#comments</comments>
		<pubDate>Thu, 02 Sep 2010 06:52:39 +0000</pubDate>
		<dc:creator>花荣</dc:creator>
				<category><![CDATA[花荣在写PHP代码]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://www.masalife.com/?p=955</guid>
		<description><![CDATA[原文网址：http://gonzalo123.wordpress.com/2010/03/15/php-and-couchdb/ 最近一直在研究couchDB。有很多API或者PHP扩展都可以帮助你使用couchDB。 在我的上一篇文章里面，已经介绍了如何用php和curl来向couchDB发送请求。 只要实现REST的HTTP请求，就可以很简单地操作couchDB。 不过呢，我最熟悉的还是关系型数据库，比如mysql啦。SQL语句写得也是刚刚的。 为什么在couchDB上就不能使用SELECT, INSERT, UPDATE和DELETE语句呢？ 因为它是NoSQL公司的产品么？ 那么我们再把上个版本里面的class封装一下吧： 在这里说明一下，原作者已经把上个版本里面的Http类改良过了，现在叫做Nov_Http，增加了一些新功能。 写一个新的class Nov_CouchDb。它的主要精力放在 HTTP Document API 上面。 关于HTTP Document API 可以看这里的文章： http://wiki.apache.org/couchdb/HTTP_Document_API 假设couchDB监听在localhost:5984，然后去 http://localhost:5984/_utils/ 创建一个&#8221;users&#8221;数据库（这一段自己操作就行了）。 继续TDD。 INSERT $couchDb = new Nov_CouchDb&#40;'localhost', 5984&#41;; $couchDb-&#62;db&#40;'users'&#41;-&#62;insert&#40;'gonzalo', array&#40;'password' =&#62; &#34;g1&#34;&#41;&#41;; SELECT $data = $couchDb-&#62;db&#40;'users'&#41;-&#62;select&#40;'gonzalo'&#41;-&#62;asArray&#40;&#41;; print_r&#40;$data&#41;; UPDATE $couchDb-&#62;db&#40;'users'&#41;-&#62;update&#40;'gonzalo', array&#40;'password' =&#62; 'g2'&#41;&#41;; DELETE $out = $couchDb-&#62;db&#40;'users'&#41;-&#62;delete&#40;'gonzalo'&#41;-&#62;asArray&#40;&#41;; print_r&#40;$out&#41;; 首先来看 Nov_CouchDb 的构造函数： class Nov_CouchDb &#123; private $_protocol; private $_host; private $_port; private $_user; private $_password; &#160; public function __construct&#40;$host, $port=Nov_Http::DEF_PORT , $protocol=Nov_Http::HTTP, $user = null, $password=null&#41; &#123; $this-&#62;_host = $host; $this-&#62;_port = $port; $this-&#62;_protocol = $protocol; $this-&#62;_user = $user; $this-&#62;_password = $password; &#125; &#125; 其实没什么好看的，保存了一下配置信息而已,并没有进行连接。 还有一个public 函数 db()&#8230;


Related posts:<ol><li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
<li><a href='http://www.masalife.com/archives/1005' rel='bookmark' title='Permanent Link: 初步学习php中的Reflection(反射)'>初步学习php中的Reflection(反射)</a></li>
<li><a href='http://www.masalife.com/archives/97' rel='bookmark' title='Permanent Link: 具有缓存功能的php rss类'>具有缓存功能的php rss类</a></li>
<li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/615' rel='bookmark' title='Permanent Link: 计算24点的程序(php版)'>计算24点的程序(php版)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>原文网址：http://gonzalo123.wordpress.com/2010/03/15/php-and-couchdb/</p>
<p>最近一直在研究couchDB。有很多API或者PHP扩展都可以帮助你使用couchDB。<br />
在我的上一篇文章里面，已经介绍了<a href='http://www.masalife.com/archives/947'>如何用php和curl来向couchDB发送请求</a>。<br />
只要实现REST的HTTP请求，就可以很简单地操作couchDB。</p>
<p>不过呢，我最熟悉的还是关系型数据库，比如mysql啦。SQL语句写得也是刚刚的。<br />
为什么在couchDB上就不能使用SELECT, INSERT, UPDATE和DELETE语句呢？<br />
因为它是NoSQL公司的产品么？</p>
<p>那么我们再把上个版本里面的class封装一下吧：<br />
<em>在这里说明一下，原作者已经把上个版本里面的Http类改良过了，现在叫做Nov_Http，增加了一些新功能。<br />
</em>写一个新的class Nov_CouchDb。它的主要精力放在 HTTP Document API 上面。<br />
关于HTTP Document API 可以看这里的文章：</p>
<p>http://wiki.apache.org/couchdb/HTTP_Document_API</p>
<p>假设couchDB监听在localhost:5984，然后去 http://localhost:5984/_utils/ 创建一个&#8221;users&#8221;数据库（这一段自己操作就行了）。</p>
<p>继续TDD。<br />
INSERT</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$couchDb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Nov_CouchDb<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5984</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gonzalo'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;g1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>SELECT</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gonzalo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>UPDATE</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gonzalo'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'g2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>DELETE</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gonzalo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>首先来看 Nov_CouchDb 的构造函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Nov_CouchDb
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_protocol</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_host</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_port</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_user</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_password</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">=</span>Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">DEF_PORT</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$protocol</span><span style="color: #339933;">=</span>Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">HTTP</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host     <span style="color: #339933;">=</span> <span style="color: #000088;">$host</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port     <span style="color: #339933;">=</span> <span style="color: #000088;">$port</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_protocol <span style="color: #339933;">=</span> <span style="color: #000088;">$protocol</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_user     <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password <span style="color: #339933;">=</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>其实没什么好看的，保存了一下配置信息而已,并没有进行连接。</p>
<p>还有一个public 函数 db()</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_db</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @param string $db
     * @return Nov_CouchDb
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> db<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>它保存了数据库名称，并且返回了对象本身。</p>
<p>好，下面来看我是如何实现SELECT INSERT DELETE UPDATE的.</p>
<p>SELECT</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> select<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_protocol<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentials</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_user<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">doGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Nov_Http_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manageExceptions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Nov_CouchDb_Resulset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>INSERT</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @param string $key
 * @param array $values
 * @return Nov_CouchDb_Resulset
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_protocol<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentials</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_user<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span>  <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">doPut</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Nov_Http_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manageExceptions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Nov_CouchDb_Resulset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>UPDATE</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @param string $key
 * @param array $values
 * @return Nov_CouchDb_Resulset
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$http</span> <span style="color: #339933;">=</span> Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_protocol<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentials</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_user<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$http</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$reg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$http</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type'</span> <span style="color: #339933;">=&gt;</span>  <span style="color: #0000ff;">'application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">doPut</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Nov_Http_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manageExceptions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Nov_CouchDb_Resulset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>DELETE:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @param string $key
 * @return Nov_CouchDb_Resulset
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$http</span> <span style="color: #339933;">=</span> Nov_Http<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_host<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_port<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_protocol<span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentials</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_user<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$http</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doGet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$reg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$http</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doDelete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;_db}</span>/<span style="color: #006699; font-weight: bold;">{$key}</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rev'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$reg</span><span style="color: #339933;">-&gt;</span>_rev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Nov_Http_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_manageExceptions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Nov_CouchDb_Resulset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这四类操作，都返回了一个 Nov_CouchDb_Resulset 对象。<br />
为什么要这样做呢？ 直接把 couchDB 的返回值获取到不就行了么？<br />
因为couchDB 返回的是json_encode过的字符串。<br />
有时候我们需要php数组，或者是个object。<br />
所以我创建了Nov_CouchDb_Resulset类来对输出进行转化：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Nov_CouchDb_Resulset
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_data</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> asArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> asJson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> asObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>如何使用：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Different outputs</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dummy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dummy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchDb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dummy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>最后再来处理一下异常：</p>
<p>自己看代码吧。<br />
代码一： </p>
<p>http://code.google.com/p/nov-couchdb/source/browse/Nov_Http.php?spec=svnf0a3289c07fbaf5cf7997f44ac0219961e4505bd&#038;r=f0a3289c07fbaf5cf7997f44ac0219961e4505bd</p>
<p>代码二： http://code.google.com/p/nov-couchdb/source/browse/Nov_CouchDb.php</p>


<p>Related posts:<ol><li><a href='http://www.masalife.com/archives/947' rel='bookmark' title='Permanent Link: 使用PHP实现REST的HTTP客户端'>使用PHP实现REST的HTTP客户端</a></li>
<li><a href='http://www.masalife.com/archives/1005' rel='bookmark' title='Permanent Link: 初步学习php中的Reflection(反射)'>初步学习php中的Reflection(反射)</a></li>
<li><a href='http://www.masalife.com/archives/97' rel='bookmark' title='Permanent Link: 具有缓存功能的php rss类'>具有缓存功能的php rss类</a></li>
<li><a href='http://www.masalife.com/archives/440' rel='bookmark' title='Permanent Link: php操作excel'>php操作excel</a></li>
<li><a href='http://www.masalife.com/archives/615' rel='bookmark' title='Permanent Link: 计算24点的程序(php版)'>计算24点的程序(php版)</a></li>
</ol></p><hr />
<p>转载请注明出处。</p>
<p><small>© 花荣 for <a href="http://www.masalife.com">花荣老师的博客</a>, 2010. |
<a href="http://www.masalife.com/archives/955">Permalink</a> |
<a href="http://www.masalife.com/archives/955#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.masalife.com/archives/955&title=PHP和couchDB">del.icio.us</a>
<br/>
Post tags: <a href="http://www.masalife.com/archives/tag/couchdb" rel="tag">couchdb</a>, <a href="http://www.masalife.com/archives/tag/php" rel="tag">php</a>, <a href="http://www.masalife.com/archives/tag/rest" rel="tag">rest</a><br/>
</small></p>
<p>
IT大腕交流群： 110443867
投稿邮箱： huarong@masalife.com
或者直接联系QQ： 190890101
</p>]]></content:encoded>
			<wfw:commentRss>http://www.masalife.com/archives/955/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
