<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>迷途知返 &#187; Techniques</title>
	<atom:link href="http://pwwang.com/category/technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://pwwang.com</link>
	<description>专注技术,用心生活!</description>
	<lastBuildDate>Wed, 08 Feb 2012 09:37:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>深入剖析php的Incomplete Object</title>
		<link>http://pwwang.com/2011/11/11/php-incomplete-object/</link>
		<comments>http://pwwang.com/2011/11/11/php-incomplete-object/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 10:18:13 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=1091</guid>
		<description><![CDATA[&#160;

行文原因

&#160;

<span class="readmore"><a href="http://pwwang.com/2011/11/11/php-incomplete-object/" title="深入剖析php的Incomplete Object">Read More: 3271 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<h1>&nbsp;</h1>
<h1>行文原因</h1>
<p>&nbsp;</p>
<p>为什么要写这篇文章? 因为我对于度娘和谷爹里搜索到关于这个问题的文章耿耿于怀, 忿忿不已. 这些文章都只说了个所以, 却没说然.</p>
<p>在这篇文章里, 我将详细地说一下Incomplete Object的成因, 以及这其中session, serialize及unserialize引起的问题和所起的作用</p>
<p><span id="more-1091"></span></p>
<h1>&nbsp;</h1>
<h1>网上给出的解决方案</h1>
<p>&nbsp;</p>
<p>1. 网上有个很典型的代码, 说是session_start()位置不正确:</p>
<p>MyClass.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: #000000; font-weight: bold;">class</span> MyClass<span style="color: #009900;">&#123;</span> 
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> increment<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>_counter<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>


<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: #990000;">session_start</span><span style="color: #009900;">&#40;</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #b1b100;">require_once</span> __DIR__ <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/MyClass.php'</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</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: #b1b100;">echo</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">increment</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: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>而给出的解决方案是:&nbsp;</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: #b1b100;">require_once</span> __DIR__ <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/MyClass.php'</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</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: #b1b100;">echo</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">increment</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: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>仔细看一下代码, 不难发现, 其实根本就不是session_start()位置不正确, 看看那个if逻辑, 在<span class="Apple-style-span" style="font-family: monospace; font-size: medium; "><span style="color: rgb(0, 0, 187); ">$_SESSION</span><span style="color: rgb(0, 119, 0); ">[</span><span style="color: rgb(221, 0, 0); ">'myObject'</span><span style="color: rgb(0, 119, 0); ">]</span></span></p>
<p>值存在时, 人为不去引用MyClass.php文件.</p>
<p>我们把if条件去掉, 不改变session_start()的位置, 程序正常运行!!!</p>
<p>2. 用serialize序列化类实例, 然后再用unserialize解序列化.</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: #990000;">session_start</span><span style="color: #009900;">&#40;</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #b1b100;">require_once</span> __DIR__ <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/MyClass.php'</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #000088;">$myObject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myObject'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myObject</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: #b1b100;">echo</span> <span style="color: #000088;">$myObject</span><span style="color: #339933;">---&gt;</span><span style="color: #004000;">increment</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: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>&nbsp;&nbsp;很显然, 问题依旧.</p>
<h1>&nbsp;</h1>
<h1>问题的真正成因</h1>
<p>&nbsp;</p>
<p>很显然, 所谓的session_start()位置, 是否用serialize序列化, 都不是这个问题真正的成因.</p>
<p>从网上给出的第一个解决方案,&nbsp; 如果我们把if条件去掉, 程序正常运行.</p>
<p>实际上, 这个if语句阻止了程序再次运行时去引用类定义文件MyClass.php.</p>
<p>这和用不用serialize和unserialize一毛钱的关系都没有.</p>
<p>&nbsp;</p>
<p>说到这里, 为了解决为什么不引用类定义文件, 而出现Incomplete Object的情况, 我们要先理解一下</p>
<p>我们把类实例(Object)传给session,那在服务器上是如何存储的呢?</p>
<p>实际上, 在服务器上, php会先把这个object序列化serialize, 再存储到文件里, 当调用的时候, 就会</p>
<p>自动反序列化unserialize,并且重新实例化这个类.</p>
<p>&nbsp;</p>
<p>那我们现在就可以解释问题的成因了. 当把object写进一个session时<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Simsun; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: monospace; "><span style="color: rgb(0, 0, 187); ">$_SESSION</span><span style="color: rgb(0, 119, 0); ">[</span><span style="color: rgb(221, 0, 0); ">'myObject'</span><span style="color: rgb(0, 119, 0); ">]&nbsp;=&nbsp;new&nbsp;</span><span style="color: rgb(0, 0, 187); ">MyClass</span><span style="color: rgb(0, 119, 0); ">()</span></span></span>,</p>
<p>把它实例化, 而在取出的时候就会自动重新实例化这个类, 而在实例化这个类的时候, 发现这个类并没有定义, 于是就会出现</p>
<p>Incomplete Object错误.</p>
<p>&nbsp;</p>
<p><strong>简单的说, 反序列化一个未定义的类的实例, 就会将object转化为Incomplete Object, 操作时就会报错</strong>.</p>
<p>&nbsp;</p>
<p>我们可以来做个实验.</p>
<p>先定义一个类, 然后获取它的实例, 再获取这个实例的序列化字符串:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'class A{var $x=0; function p(){echo ++$x;}} $a=new A(); echo serialize($a);'</span>
O:<span style="color: #000000;">1</span>:<span style="color: #ff0000;">&quot;A&quot;</span>:<span style="color: #000000;">1</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span>s:<span style="color: #000000;">1</span>:<span style="color: #ff0000;">&quot;x&quot;</span>;i:<span style="color: #000000;">0</span>;<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>我们来反序列化这个字符串, 并调用p方法:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'$a=unserialize(&quot;O:1:\&quot;A\&quot;:1:{s:1:\&quot;x\&quot;;i:0;}&quot;);$a-&gt;p();'</span></pre></div></div>

<p>最后我们看到报出Incomplete Object的错误:</p>
<p>&nbsp;</p>
<blockquote>
<p>Fatal error: Unknown: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;A&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition&nbsp; in Command line code on line 1</p>
</blockquote>
<div>如果我们在反序列化这个字符串之前, 给出类A的声明:</div>
<div>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'class A{var $x=0; function p(){echo ++$x;}} $a=unserialize(&quot;O:1:\&quot;A\&quot;:1:{s:1:\&quot;x\&quot;;i:0;}&quot;);$a-&gt;p();'</span>
<span style="color: #000000;">1</span></pre></div></div>

<p>那么这个程序就可以正常运行.</p></div>
<div>&nbsp;</div>
<div>其实session的情况 同样也适用于将object序列化后存入文件, 然后再从文件中取出反序列化的情况.</div>
<div>&nbsp;</div>
<div>虽然, 如果我们不执行p方法, 程序不会报错, 但是在反序列化的时候, 如果类没有定义, 那么这个object就会是一个Incomplete Object了:</div>
<div>看下面的代码:</div>
<div>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'$a=unserialize(&quot;O:1:\&quot;A\&quot;:1:{s:1:\&quot;x\&quot;;i:0;}&quot;);print_r($a);'</span>
__PHP_Incomplete_Class Object
<span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #7a0874; font-weight: bold;">&#91;</span>__PHP_Incomplete_Class_Name<span style="color: #7a0874; font-weight: bold;">&#93;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> A
    <span style="color: #7a0874; font-weight: bold;">&#91;</span>x<span style="color: #7a0874; font-weight: bold;">&#93;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

</div>
<h1>解决办法</h1>
<p>&nbsp;</p>
<p>其实真正的解决办法, 上面已经给出来了, 就是在反序列化之前引入声明这个类, 或者引入这个类的实例.</p>
<p>为了避免session在取值自动反序列化将object转成Incomplete Object, 这个时候, 我们就需要显示地去serialize一个object, 然后我们取值时</p>
<p>取到的只是序列化的字符串, 这时php是不会去检测类是否定义的. 那么我们就可以在这个时候解决问题了.</p>
<p>&nbsp;</p>
<p>下面给出一个解决方案:</p>
<p>假设命名规则是这样的A.php文件定义了类A, 而我们在session中,以小写a为key来保存:</p>
<p>&nbsp;</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: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// try to include class declaration file before use it</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</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;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// TODO: whatever you what to do with $obj ...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>当然, 你也可以用auto_loader来加载类的声明文件.</p>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin"></embed></div>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" type="application/lingoes-npruntime-capture-word-plugin" id="lingoes_plugin_object"></embed></div>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin"></embed></div>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" type="application/lingoes-npruntime-capture-word-plugin" id="lingoes_plugin_object"></embed></div>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin"></embed></div>
<p>&nbsp;</p>
<div><embed width="0" hidden="true" height="0" type="application/lingoes-npruntime-capture-word-plugin" id="lingoes_plugin_object"></embed></div>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/11/11/php-incomplete-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LAMP流程中的乱码问题总结</title>
		<link>http://pwwang.com/2011/07/12/kill-messy-codes/</link>
		<comments>http://pwwang.com/2011/07/12/kill-messy-codes/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 19:28:43 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pwwang.com/2011/07/12/lamp%e6%b5%81%e7%a8%8b%e4%b8%ad%e7%9a%84%e4%b9%b1%e7%a0%81%e9%97%ae%e9%a2%98%e6%80%bb%e7%bb%93/</guid>
		<description><![CDATA[这里说的乱码问题包括：

一，开发的页面出现乱码，

二，从数据库取值或插入时出现乱码，

<span class="readmore"><a href="http://pwwang.com/2011/07/12/kill-messy-codes/" title="LAMP流程中的乱码问题总结">Read More: 827 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>这里说的乱码问题包括：</p>
<p>一，开发的页面出现乱码，</p>
<p>二，从数据库取值或插入时出现乱码，</p>
<p>三，phpmysql页面出现乱码。</p>
<p><span id="more-1080"></span></p>
<p>&nbsp;</p>
<p><!-- wpmore --><!-- more --></p>
<p>刚开始碰到乱码问题可能会不知所措，但是了解了可能出现乱码的几个环节，便可以轻松地进行一一排查。</p>
<p>&nbsp;</p>
<p>避免出现乱码的最好的做法，就是在所有指定编码的地方指定相同的编码(废话，乱码不就是编码不一致造成的么？)。这里推荐釆用utf-8进行统一编码。</p>
<p>下面分别描述及解释下上面三种出现乱码的情况。</p>
<p>第一种情况，不涉及数据库操作，或者我们假设数据库的操作不会造成乱码（造成乱码的情况下面讨论）。那么只需要检查脚本文件的编码和页面编码是否一致即可。文件的编码一般的文本编辑器都可以更改，如notepad2，notepad++等，而网页页面的编码则通过meta标签来指定。<br />
&lt;meta content=&#8221;text/html; charset=UTF-8&#8243; http-equiv=&#8221;Content-Type&#8221; /&gt;
</p>
<p>第二种情况，则需要数据库编码、数据库表中该字段的编码及页面编码保持一致。数据库编码及字段编码可以在创建时设置collation来指定。而页面编码上面已经提到。</p>
<p>有的时候，我们在进行数据库操作时，插入的是什么字符，读取出来的时候还是那些字符，好像没有什么问题。但是当我们对数据库进行管理的时候，在第三方工具里显示的却是乱码，如phpmysql,mysql workbench等。这就是由于连接数据库时的编码和数据库的编码或者phpmysql的页面编码不一致造成的。我们可以在数据库连接建立后通过sql语句来指定编码：SET NAMES UTF8; 注意没有连字符。</p>
<p>总结：这里推荐的方式是将所有的编码指定为utf-8。</p>
<p>脚本文件编码：utf-8</p>
<p>网页页面编码：<br />
&lt;meta content=&#8221;text/html; charset=UTF-8&#8243; http-equiv=&#8221;Content-Type&#8221; /&gt;
</p>
<p>数据库及字段编码：utf8_general_ci</p>
<p>数据库连接编码：SET NAMES UTF8;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/07/12/kill-messy-codes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>随机记录分页</title>
		<link>http://pwwang.com/2011/05/20/random-records-pagination/</link>
		<comments>http://pwwang.com/2011/05/20/random-records-pagination/#comments</comments>
		<pubDate>Fri, 20 May 2011 05:38:04 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[分页]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=1037</guid>
		<description><![CDATA[&#160;

问题（需求）：

1. 每次打开第一页时，显示的记录不同。

<span class="readmore"><a href="http://pwwang.com/2011/05/20/random-records-pagination/" title="随机记录分页">Read More: 3925 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>问题（需求）：</p>
<p>1. 每次打开第一页时，显示的记录不同。</p>
<p>2. 但是从第一页后的其他页面（第二页，第三页，。。。）返回第一页时，还是那个第一次打开第一页的那些记录</p>
<p>3. 每页记录不能重复</p>
<p>&nbsp;</p>
<p>其实看起来很简单的一个问题，但是加上分页的要求之后，变得稍微有点复杂。</p>
<p>如果是随机从数据库取一条记录，最直接的方式，就是order by rand() limit 1:</p>
<p><span id="more-1037"></span></p>
<p>当然，网上也有很多提高order by rand()效率的写法，都可以参考。</p>
<p>如果是不需要分页，那order by rand()及其优化方式，也都可以满足需求。</p>
<p>倘若需要分页，我们给rand指定一个参数，再配以limit，可以满足我们第2，3条要求。</p>
<p>因为如果每次请求rand的参数是不变的，那每次取的记录顺序也是一样的，这样每次每开第一页的记录，自然也是一样的。</p>
<p>重新连接数据库也一样。</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">3</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> Bye
pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">4</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> Bye</pre></div></div>

<p>&nbsp;</p>
<p>重启数据库，结果还是一样</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> lampp restart
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">sudo</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> password <span style="color: #000000; font-weight: bold;">for</span> pwwang:
Stopping XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux 1.7.4...
XAMPP: Stopping Apache with SSL...
XAMPP: Stopping MySQL...
XAMPP: Stopping ProFTPD...
XAMPP stopped.
Starting XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux 1.7.4...
XAMPP: Starting Apache with SSL <span style="color: #7a0874; font-weight: bold;">&#40;</span>and PHP5<span style="color: #7a0874; font-weight: bold;">&#41;</span>...
XAMPP: Starting MySQL...
XAMPP: Starting ProFTPD...
XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux started.
pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">1</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>因为给定参数之后，RAND的结果是一样的</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>             <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0.40540353712197724</span> <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000;">1</span> row <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.02</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>             <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0.40540353712197724</span> <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000;">1</span> row <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>如果我们每次在打开第一页的时候就给RAND一个随机的参数，那问题就可以解决了。</p>
<p>&nbsp;</p>
<p><strong><span style="color: rgb(0, 128, 128);"><span style="font-size: larger;">Solution 1：</span></span></strong></p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$seed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'seed'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 你也可以用session, cookie等方式来存储这个值</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'seed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
    <span style="color: #000088;">$seed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM table ORDER BY RAND(<span style="color: #006699; font-weight: bold;">$seed</span>) LIMIT <span style="color: #006699; font-weight: bold;">$offset</span>, <span style="color: #006699; font-weight: bold;">$limit</span>;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>页面渲染的时候每个和分页相关的链接都带上参数seed=$seed, 就可以保证在这个周期的记录浏览中，每一页的记录都是特定的。</p>
<p>在下个周期中，由于没有$_GET['seed']的值，$seed会重新随机取值，那么，第一页的记录也就和上个周期不一样了。</p>
<p>&nbsp;</p>
<p><strong><span style="color: rgb(0, 128, 128);"><span style="font-size: larger;">Solution 2：</span></span></strong></p>
<p>当然，也有人说到了order by rand() 的效率问题。那我们可以用脚本事先把primary key（假设这里是increment的id）排好随机顺序，再传给sql去查询。</p>
<p>如果你的记录的id是连续的，你可以用max(id)来获取pk的序列：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'serial'</span><span style="color: #009900;">&#93;</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;">$serial</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$maxid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_value</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT MAX(id) FROM table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$serial</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$ids_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$serial</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ids_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM table WHERE id IN (<span style="color: #006699; font-weight: bold;">$ids</span>);&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果你单页的记录不多的话（一般也不会太多），这个替代方案可以很好的弥补order by rand的效率问题</p>
<p>&nbsp;</p>
<p>但是如果你的记录id不是连续的，那么就想要办法先拿到id的序列：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_value</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT group_concat(id) FROM table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$serial</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这里只演示了如何获取id序列。其它机制和连续id是一样的。</p>
<p>&nbsp;</p>
<p>不过这里用到了group_concat，来选取所有记录的id。</p>
<p>效率问题，大家可以根据实际情况在order by rand和group_concat之间权衡。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/05/20/random-records-pagination/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WP-Plugin: Verification Code for Comments 2.1.0</title>
		<link>http://pwwang.com/2011/04/17/verification-code-for-comments-2/</link>
		<comments>http://pwwang.com/2011/04/17/verification-code-for-comments-2/#comments</comments>
		<pubDate>Sun, 17 Apr 2011 04:35:38 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[WP/WP Plugins]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[WP插件]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=1011</guid>
		<description><![CDATA[&#160;

Version 2.1.0

* remove jquery dependency

<span class="readmore"><a href="http://pwwang.com/2011/04/17/verification-code-for-comments-2/" title="WP-Plugin: Verification Code for Comments 2.1.0">Read More: 1019 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p><span style="font-size: x-large;"><span style="color: rgb(0, 128, 128);"><strong>Version 2.1.0</strong></span></span></p>
<p>* remove jquery dependency<br />
* re-orgnize the js code<br />
&nbsp;</p>
<p><span style="font-size: x-large;"><strong><span style="color: rgb(0, 128, 128);">Version 2.0.2</span></strong></span></p>
<p>Add an verification code when user posting a comment to keep robots away. You can use an image verification code or a math equation instead.</p>
<p>Robots may post lots of spam comments into your database. You can add a verification code image or a math equation to avoid this.</p>
<p>Features:</p>
<ul>
<li>You can choose a verification code image or a math equation as you wish</li>
<li>You don&#8217;t need to edit any source code of WP, you just need to active the plugin&nbsp;</li>
</ul>
<ol class="screenshots">
<li><strong>&nbsp;configuration panel<br />
    </strong><br />
    <img width="640" height="921" src="/wp-content/plugins/verification-code-for-comments/screenshot-1.png" style="clear: both" alt="" /><br />
    <span id="more-1011"></span>&nbsp;</li>
<li><strong>&nbsp;a verification code image
<p>    </strong><img width="433" height="218" src="/wp-content/plugins/verification-code-for-comments/screenshot-2.png" style="clear: both" alt="" /><strong></p>
<p>    </strong></li>
<li><strong>&nbsp;a verification math equation
<p>    </strong><img width="423" height="208" src="/wp-content/plugins/verification-code-for-comments/screenshot-3.png" style="clear: both" alt="" /><strong><br />
    </strong><br />
    &nbsp;</li>
<li><strong>&nbsp;the error message when invalid code was entered
<p>    </strong><img width="434" height="224" src="/wp-content/plugins/verification-code-for-comments/screenshot-4.png" style="clear: both" alt="" /><strong></p>
<p>    </strong></li>
</ol>
<p>&nbsp;</p>
<p>&nbsp;<span style="font-size: small"><strong>Change Log:</strong></span></p>
<p>1.0.0&nbsp;&nbsp; Finish the basic functions</p>
<p>2.0.0</p>
<p><span style="color: #ff0000">* Change the skin of code and input box<br />
* Position of the box can be choosen<br />
* Number of characters can be set<br />
* Fix the bug in Chrome that code does not appear<br />
* Case sensitivity is configurable now for string mode<br />
* Length of string is configurable now for string mode<br />
* Position os verification code box could be coufigurable now</span></p>
<p>2.0.1, 2.0.2</p>
<p>* bug fixed</p>
<p>2.1.0<br />
* remove jquery dependency<br />
* re-orgnize the js code<br />
&nbsp;</p>
<p><strong><span style="font-size: small">Download/Install:</span></strong></p>
<p><a href="http://wordpress.org/extend/plugins/verification-code-for-comments/" target="_blank">http://wordpress.org/extend/plugins/verification-code-for-comments/</a></p>
<p>&nbsp;</p>
<p>Explanation to the configuration:</p>
<p><img width="652" height="408" src="http://pwwang.com/wp-content/uploads/2011/04/vcc_setting.png" style="clear:both" title="vcc_setting" alt="" /></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/04/17/verification-code-for-comments-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>WebQQ桌面化软件pyWebQQ for Ubuntu</title>
		<link>http://pwwang.com/2011/04/07/pywebqq1-0-for-ubuntu/</link>
		<comments>http://pwwang.com/2011/04/07/pywebqq1-0-for-ubuntu/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 03:07:30 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[pyWebQQ]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=998</guid>
		<description><![CDATA[&#160;

十分抱歉, 由于Ubuntu 11.10不再提供库gtkmozembed, 请11.10以及上的童鞋不要安装, 本软件也由于本人时间精力有限, 暂停更新.

软件是开源的, 有能力的童鞋可以任意更改, 发布, 如果您觉得本人做了少少贡献, 可以考虑加上本站地址, 谢谢.

<span class="readmore"><a href="http://pwwang.com/2011/04/07/pywebqq1-0-for-ubuntu/" title="WebQQ桌面化软件pyWebQQ for Ubuntu">Read More: 1081 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p><span style="color: rgb(255, 0, 0); "><strong><span style="font-size: medium; ">十分抱歉, 由于Ubuntu 11.10不再提供库gtkmozembed, 请11.10以及上的童鞋不要安装, 本软件也由于本人时间精力有限, 暂停更新.</span></strong></span></p>
<p><span style="color: rgb(255, 0, 0); "><strong><span style="font-size: medium; ">软件是开源的, 有能力的童鞋可以任意更改, 发布, 如果您觉得本人做了少少贡献, 可以考虑加上本站地址, 谢谢.</span></strong></span></p>
<p><span style="color: rgb(0, 128, 128); "><strong>======================== VERSION 1.2 =====================</strong>&nbsp;</span></p>
<p>随WebQQ版本更新 至1.2, 解决启动时就弹出消息的问题.</p>
<p>下载地址:&nbsp;<a href="http://pwwang.com/pywebqq-downloa/"><span id="sample-permalink">pyWebQQ 1.2</span></a></p>
<p><span style="color: rgb(0, 128, 128); "><strong>======================== VERSION 1.1 =====================</strong>&nbsp;</span></p>
<p>随WebQQ版本更新 至1.1, 解决WebQQ3.0标题不停弹出的问题.</p>
<p>( 最近有点忙, 大家提的问题, 等我稍闲下来再解决, 请见谅. )</p>
<p>下载地址:&nbsp;<a href="http://pwwang.com/pywebqq-downloa/"><span id="sample-permalink">pyWebQQ 1.1</span></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>如果已经安装了1.0版本，建议先sudo make uninstall卸载，再用1.0的步骤安装。</p>
<p>当然覆盖安装也没有任何问题。</p>
<p>另外，关于Notify请使用我包里提供的，最新版本有bug（<strong>感谢</strong>krisyouxi<strong>童鞋的提醒</strong>）。</p>
<p><span style="color: rgb(0, 128, 128); "><strong>======================== VERSION 1.0 =====================</strong></span></p>
<p>因为工作需要, 工作机器装了ubuntu.</p>
<p>ubuntu下用qq是个老大难题.</p>
<p>万恶的TX总是在其服务端加些验证以至第三方软件无法正常登录qq,</p>
<p>包括Pidgin,Empathy.</p>
<p>&nbsp;</p>
<p>有人提议把WebQQ桌面化,这个主意还不错.</p>
<p>网上多半的方法是这样的:</p>
<p>利用Mozilla的prism或者Chrome自带的功能生成桌面应用程序,</p>
<p>然后再利用Alltray或者KDocker来将应用程序放到系统托盘.</p>
<p>我各种组合都试过, 各方面都很好,就是无法提示新消息.</p>
<p>&nbsp;</p>
<p>一怒之下&#8230; 自己用Python写了一个桌面版的WebQQ, 暂时称之为pyWebQQ.</p>
<p>功能如下 :</p>
<p>1. 完整包含WebQQ的所有功能</p>
<p>2. 来新消息气泡提示, 并且图标闪烁</p>
<p>3. 安装时自动check环境</p>
<p>4. 安装后自动生成桌面图标</p>
<p>上图</p>
<p><img width="600" height="375" alt="" style="clear:both" title="Screenshot1" src="http://pwwang.com/wp-content/uploads/2011/04/Screenshot1.jpg" /></p>
<p><span id="more-998"></span></p>
<p><img width="600" height="375" alt="" style="clear:both" title="Screenshot2" src="http://pwwang.com/wp-content/uploads/2011/04/Screenshot2.jpg" /></p>
<p>&nbsp;</p>
<p><img width="600" height="375" alt="" style="clear:both" title="Screenshot" src="http://pwwang.com/wp-content/uploads/2011/04/Screenshot.jpg" /></p>
<p>&nbsp;</p>
<p>安装包里已经包含了pynotify这个module, gtk和gtkmozembed源里有下载</p>
<p>gtk就不用说了, gtkmozembed可以用这个命令安装: sudo apt-get install python-gtkmozembed</p>
<p>安装方法:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure.py
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">make</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>&nbsp;</p>
<p>顺带提一下pynotify的安装方法:</p>
<p>进入pynotify目录,然后执行:</p>
<p>./configure &amp; make &amp; sudo make install</p>
<p>下载地址: <a href="http://pwwang.com/pywebqq-downloa/"><span id="sample-permalink">pyWebQQ 1.0</span></a></p>
<p>&nbsp;<br />
<meta content="text/html; charset=utf-8" http-equiv="content-type"><br />
</meta>
</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/04/07/pywebqq1-0-for-ubuntu/feed/</wfw:commentRss>
		<slash:comments>120</slash:comments>
		</item>
		<item>
		<title>js控制表单提交经典一例</title>
		<link>http://pwwang.com/2011/04/06/js_html_cannon_problem/</link>
		<comments>http://pwwang.com/2011/04/06/js_html_cannon_problem/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 14:11:43 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=994</guid>
		<description><![CDATA[扯句闲话，不知道为什么，偶没写什么反动言论啊，IP被墙，换IP后国内才能访问。。。搞得上篇都整E文，心想反正国人看不到了。。。现在终于可以用上母语了。。。

言归正传。

前两天和同事调程序，遇上js控制表单提交经典案例。

<span class="readmore"><a href="http://pwwang.com/2011/04/06/js_html_cannon_problem/" title="js控制表单提交经典一例">Read More: 654 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>扯句闲话，不知道为什么，偶没写什么反动言论啊，IP被墙，换IP后国内才能访问。。。搞得上篇都整E文，心想反正国人看不到了。。。现在终于可以用上母语了。。。</p>
<p>言归正传。</p>
<p>前两天和同事调程序，遇上js控制表单提交经典案例。</p>
<p>事情是这样的，他用js控制表单提交，结果发现无论如何也无论提交数据。但是点submit按钮却可以。</p>
<p>他的表单大致如此：</p>
<p><span id="more-994"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;www.google.com&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;property&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span>=submit <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;sub&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;javascript:this.form.submit()&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
<p>表现就是 点submit可以提交，点sub无法提交。。。</p>
<p>于是怀疑form的submit函数有问题，尝试打印出form的所有属性，发现submit的值是字符串submit，</p>
<p>而不是一个function，看到submit的name后，恍然大悟，</p>
<p>button的name值设为submit后，它成为了form的一个属性，从而覆盖了form原有的submit函数。。</p>
<p>修正方法也很简单，将submit的name改为其他值就可以了。</p>
<p>变量名有意识的避开潜在关键字，还是一个挺好的习惯。</p>
<p>正是因为我之前在写表单的时候都避开了类似的关键字（从未出过类似问题），</p>
<p>所以看到他的表单的submit的时候只是觉得有点不妥，却没有警觉起来。</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/04/06/js_html_cannon_problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Call global variables in PHP and Python</title>
		<link>http://pwwang.com/2011/03/14/call-global-variables-in-php-and-python/</link>
		<comments>http://pwwang.com/2011/03/14/call-global-variables-in-php-and-python/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 03:14:57 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=988</guid>
		<description><![CDATA[&#160;

Call global variables in PHP

we need to declare the variables with &#8216;global&#8217; statement.

<span class="readmore"><a href="http://pwwang.com/2011/03/14/call-global-variables-in-php-and-python/" title="Call global variables in PHP and Python">Read More: 398 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>Call global variables in PHP</p>
<p>we need to declare the variables with &#8216;global&#8217; statement.</p>
<p>&nbsp;</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: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</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;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>&nbsp;</p>
<p>While in Python, it&#8217;s not necessary to declare the global variables when reading it</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">var = <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> var</pre></div></div>

<p>&nbsp;</p>
<p>However, if you&#8217;d like to change the variables, you need to declare it as same as in PHP</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">var = <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">global</span> var
    var = var + <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> var</pre></div></div>

<p>&nbsp;</p>
<p>Otherwise, an error like &quot;local variable &#8216;xxx&#8217; referenced before assignment&quot; will occur</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/03/14/call-global-variables-in-php-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A more elegant way to write OO-JavaScript</title>
		<link>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/</link>
		<comments>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 14:01:35 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=978</guid>
		<description><![CDATA[&#160;

JavaScript IS an OO language itself.

I just wrote a jQuery plugin to realize a C-like class writing style.

<span class="readmore"><a href="http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/" title="A more elegant way to write OO-JavaScript">Read More: 2731 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>JavaScript IS an OO language itself.</p>
<p>I just wrote a jQuery plugin to realize a C-like class writing style.</p>
<p>I&#8217;ve used just one of the jQuery functions, $.extend. So if you&#8217;d like to make it standalone, just realize $.extend yourself then you make it.</p>
<p>Now see the code, I&#8217;ve made comments in it.</p>
<p><span id="more-978"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * @Author: pwwang(pwwang@pwwang.com)
 * @Function: make a more elegant way to write OO-JavaScript
 * @Note: presevered member name: 
 *   _init -&gt; constructor, 
 *   _super -&gt; super class which this class inherited.
 **/</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    $.<span style="color: #003366; font-weight: bold;">Class</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>opts<span style="color: #339933;">,</span> ext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//deepcopy, opts won't change</span>
        <span style="color: #003366; font-weight: bold;">var</span> temp_opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> temp_opts<span style="color: #339933;">,</span> opts <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #006600; font-style: italic;">// temporarily store the super class</span>
        <span style="color: #003366; font-weight: bold;">var</span> _super <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> ext <span style="color: #339933;">&amp;&amp;</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>ext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// deepcopy ext.prototype that it won't change</span>
            <span style="color: #003366; font-weight: bold;">var</span> ext_prttype <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> ext_prttype<span style="color: #339933;">,</span> ext.<span style="color: #660066;">prototype</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// class extension</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> ext_prttype<span style="color: #339933;">,</span> temp_opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// give it back to temp_opts</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> temp_opts<span style="color: #339933;">,</span> ext_prttype <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// get ext.prototype store in _super</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> _super<span style="color: #339933;">,</span> ext.<span style="color: #660066;">prototype</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// initialize this.super</span>
            temp_opts._super <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #003366; font-weight: bold;">CLASS</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// class initialization</span>
            <span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span>._init.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>t<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// realize this.super, try to apply THIS to the functions.</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> _super <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                temp_opts._super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>_super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> 
                <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>t<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>_super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> _super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// set other variables or functions</span>
        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> temp_opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// use static impl to set</span>
        <span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">impl</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>im<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> im<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">CLASS</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>Here are some samples to show how to use it.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.class.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
    var log = function(v){
        (window.console &amp;&amp; window.console.log) ? window.console.log(v) : window.alert(v);
    }
&nbsp;
    var Anim = $.Class({
        _init: function(weight){
            this.weight = weight;
        },
        eat: function(){
            this.weight ++;
        },
        excrete: function(){
            this.weight --;
        },
        getWeight: function(){
            return this.weight;
        }
    });
&nbsp;
    var Cat = $.Class({
        _init: function(name, weight){ // override
            this.name = name;
            this.weight = weight;
        }
    }, Anim); // Inheritance 
&nbsp;
    Cat.impl({
        exercise : function(){
            this.weight -= 2;
        },
        eat: function(){
            if( arguments.length ){ // polymorphism
                this.weight += arguments[0];
            } else {
                this._super.eat();
            }
        }
    });
&nbsp;
    var Dog = $.Class({
        _init: function(name, weight){
            this.name = name;
            this.weight = weight;
        },
        eat: function(){
            this.weight += 2;
        }
    }, Anim);
&nbsp;
    var myAnim = new Anim(50);
    log(myAnim.getWeight()); // 50
    myAnim.eat();
    log(myAnim.getWeight()); // 51
    myAnim.excrete();
    log(myAnim.getWeight()); // 50
&nbsp;
    var myCat = new Cat('cat', 50);
    log(myCat.getWeight()); // 50
    myCat.eat();
    log(myCat.getWeight()); // 51
    myCat.eat(2);
    log(myCat.getWeight()); // 53
    myCat.excrete();
    log(myCat.getWeight()); // 52
    myCat.exercise();
    log(myCat.getWeight()); // 50
&nbsp;
    var myDog = new Dog('dog', 50);
    log(myDog.getWeight()); // 50
    myDog.eat();
    log(myDog.getWeight()); // 52
    myDog.excrete();
    log(myDog.getWeight()); // 51
&nbsp;
&nbsp;
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>自己动手解析xml&#8211;原理篇</title>
		<link>http://pwwang.com/2010/09/03/parse_xml_principle/</link>
		<comments>http://pwwang.com/2010/09/03/parse_xml_principle/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 05:26:53 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[解析xml]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=971</guid>
		<description><![CDATA[&#160;

没错, 有人说, 你这不是纯粹吃饱了撑着吗?

是的, xml作为数据载体拥有众多优势, 使得其应用越来越广泛.

<span class="readmore"><a href="http://pwwang.com/2010/09/03/parse_xml_principle/" title="自己动手解析xml&#8211;原理篇">Read More: 1130 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>没错, 有人说, 你这不是纯粹吃饱了撑着吗?</p>
<p>是的, xml作为数据载体拥有众多优势, 使得其应用越来越广泛.</p>
<p>越来越多的语言也自带了很多xml解析库.</p>
<p>但是到底是如何解析那一堆xml字符串的, 少有人探究.</p>
<p>我也无从得知, 久前也尝试自己解析过, 但是方法很笨拙.</p>
<p>前两天从盒子里放着的小盒子, 我想到了稍好一些的解析方法.</p>
<p><span id="more-971"></span><strong>-&gt; 理解xml的结构</strong></p>
<p>&nbsp; 每一个xml的标签比较是闭合的, 可以嵌套, 但不可以交叉.比如这样的结构是可以的:&nbsp;&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
<p>&nbsp; 但是这样是不被允许的:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/b<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;b<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
<p>&nbsp; 每一个xml文件只能有一个根.</p>
<p>&nbsp; 每个标签可以有自己的属性, 这个和HTML类似</p>
<p>&nbsp; 标签的值被其开始标签和结束标签所包围</p>
<p>&nbsp; 如果值比较复杂,或者其中含有打乱xml结构的字符, 可以采用cdata</p>
<p>&nbsp; 如果标签没有值, 可以采用这种方式简写:</p>
<p>&nbsp;</p>

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

<p>&nbsp;</p>
<p><strong>-&gt; 精妙的比喻</strong></p>
<p>&nbsp; 这个比喻是从那两个盒子想到的.</p>
<p>&nbsp; 每个盒子必须有盖子(标签闭合)</p>
<p>&nbsp; 小盒子可以放在大盒子里(标签可以嵌套)</p>
<p>&nbsp; 每个盒子的盖子必须盖在自己的盒身上(不能交叉)</p>
<p>&nbsp; 现在有一堆盒子, 我们要把它们按照指定的关系(谁放在谁里面, 谁放在谁外面)整理好, 放在一个大盒子(那个唯一的根)里拎走.</p>
<p>&nbsp; 我们考虑的, 就是这个放置的过程.</p>
<p>&nbsp; 可以很直观的发现:&nbsp;&nbsp; 先放的盒子后盖盖, 后放的盒子先盖盖, 如果里面不用放盒子了, 直接盖上盖.</p>
<p>&nbsp; (-为什么? -什么, 你问我为什么? 那..那&#8230;我问谁去?! 呃, 如果你有这个疑问, 建议你不要往下看了)</p>
<p>&nbsp; (-哇! -嘘! 现在是偶在说话, 就算你发现了秘密, 也要等偶说完嘛. 如果你发出了这样的惊叹, 请继续往下看)</p>
<p>&nbsp; 看到这里, 相信你已经能够把这个过程和xml的解析联系起来了吧.</p>
<p>&nbsp; 如果我们把这个整理例子的过程看成是我们还读/写整个xml文件的过程, 那么就是这样:</p>
<p>&nbsp; 先读到的标签, 一定是比后读到的标签后闭合.</p>
<p><strong>-&gt;如何解析?</strong></p>
<p>&nbsp; 好了,&nbsp; 早就有人不耐烦了, 终于到正题了, 说了半天, 你还是没说怎么解析嘛.</p>
<p>&nbsp; 先读到的后闭合, 先读到的后闭合&#8230;&#8230;</p>
<p>&nbsp; 我想到了, 你想到了么? 是什么? 堆栈!! 没错, 就是堆栈!</p>
<p>&nbsp; 容易了吧, 容易了吧, 简单了吧, 简单了吧&#8230;</p>
<p>&nbsp; 读xml的字符串, 每读到一个开始标签, 压入堆栈, 每读到一个闭合标签, 弹出栈顶元素, 那么, 现在的栈顶元素就是它的直接父节点.</p>
<p>&nbsp; (-为什么,为什么,为什么? -哪来那么多为什么?! 盖上盖子, 先于这个盒子放进去, 且没有盖盖子的盒子, 是不是它的父盒子? 是不是, 是不是?</p>
<p>&nbsp; 注意这里没有盖盖子很重要, 因为盖上了盖子就不能往里放盒子了, 也没不可能有这个子盒子了, 因为盖上盖子就弹出堆栈了.)</p>
<p>&nbsp; 元素得到了, 它的父节点也得到了(相当于得到了子节点(父节点的子节点)), 那么所有的元素都串起来了, 解析也就简单了.</p>
<p>&nbsp;<a href="http://pwwang.com/wp-content/uploads/2010/09/xml.jpg"><img width="470" height="149" border="0" alt="" style="clear: both;" src="http://pwwang.com/wp-content/uploads/2010/09/xml.jpg" title="xml" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2010/09/03/parse_xml_principle/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>简单写了个jquery的鼠标提示文字插件</title>
		<link>http://pwwang.com/2010/08/27/jquery_tip/</link>
		<comments>http://pwwang.com/2010/08/27/jquery_tip/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 01:51:07 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=968</guid>
		<description><![CDATA[&#160;

效果图:



<span class="readmore"><a href="http://pwwang.com/2010/08/27/jquery_tip/" title="简单写了个jquery的鼠标提示文字插件">Read More: 1137 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>效果图:</p>
<p><a href="http://pwwang.com/wp-content/uploads/2010/08/jquery.tip_.js.jpg"><img width="347" height="198" title="jquery.tip.js" src="http://pwwang.com/wp-content/uploads/2010/08/jquery.tip_.js.jpg" style="clear: both;" alt="" /></a></p>
<p><span id="more-968"></span>调用:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/jquery.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/jquery.tip.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
$(function(){
    $('div').tip('This is a multipul style tip. I\'m <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;font</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;font-weight:bold&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bold<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/font<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>Line break. I\'m <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;font</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;color:red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>red<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/font<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>. Images are also availale. <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://pwwang.com/logo.gif&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>');
});
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float:left; width:300px; height:300px; background:#f90;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>handler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</p>
<p>源码:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    $.<span style="color: #660066;">tip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> $tdiv <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">'position'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'absolute'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'display'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'z-index'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'9999'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> cls <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">'border'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'1px solid #969696'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'background'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffe'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'font-size'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'12px'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'padding'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'2px'</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">'top'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">pageY</span><span style="color: #339933;">+</span><span style="color: #CC0000;">23</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'left'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">pageX</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> move<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseleave</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> move<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// content: what is the tip</span>
    <span style="color: #006600; font-style: italic;">// cls: the style class of the tip div</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">tip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">new</span> $.<span style="color: #660066;">tip</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2010/08/27/jquery_tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

