<?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>IT狂人的博客 &#187; 译文</title>
	<atom:link href="http://www.yinzhili.com/category/translations/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yinzhili.com</link>
	<description>世界上有10种人:一种是懂二进制的，另一种是不懂的。</description>
	<lastBuildDate>Thu, 22 Jul 2010 03:51:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>使用PHP的Glob()函数来遍历文件夹</title>
		<link>http://www.yinzhili.com/2010/07/loop-through-folders-with-phps-glob.html</link>
		<comments>http://www.yinzhili.com/2010/07/loop-through-folders-with-phps-glob.html#comments</comments>
		<pubDate>Thu, 22 Jul 2010 03:51:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[译文]]></category>
		<category><![CDATA[glob]]></category>
		<category><![CDATA[opendir]]></category>
		<category><![CDATA[遍历]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=283</guid>
		<description><![CDATA[<div style="color:#2d65a1;margin-bottom:10px;">原创译文，转载请注明出处。原作者为Marcus Schumann，原文请 <a href="http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/" target="_blank">单击这里</a> 查看。</div>
你还在使用 opendir() 来遍历文件夹吗？那样岂不是会生成很多冗余的代码？幸运的是，PHP的 glob() 是一个更加智能的解决方案。
<div style="font-size:18px;font-weight:bold;">引言</div>
下面这个范例的作用就是输出文件夹的一些信息，使用的是传统的 [......]<p class='read-more'><a href='http://www.yinzhili.com/2010/07/loop-through-folders-with-phps-glob.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<div style="color:#2d65a1;margin-bottom:10px;">原创译文，转载请注明出处。原作者为Marcus Schumann，原文请 <a href="http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/" target="_blank">单击这里</a> 查看。</div>
<p>你还在使用 opendir() 来遍历文件夹吗？那样岂不是会生成很多冗余的代码？幸运的是，PHP的 glob() 是一个更加智能的解决方案。</p>
<div style="font-size:18px;font-weight:bold;">引言</div>
<p>下面这个范例的作用就是输出文件夹的一些信息，使用的是传统的 opendir() 函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/etc/php5/&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Open a known directory, and proceed to read its contents</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dh</span><span style="color: #009900;">&#41;</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;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;filename: <span style="color: #006699; font-weight: bold;">$file</span> : filetype: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">filetype</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>那看起来应该有点熟悉。我们可以大幅度地精简以上的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/etc/php5/*&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Open a known directory, and proceed to read its contents</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;filename: <span style="color: #006699; font-weight: bold;">$file</span> : filetype: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">filetype</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>是不是简单多了？很想了解这方法是怎么运行的吗？下面我们就来讲解一下。<br />
glob() 一共支持两个参数，第二个参数是可选的。第一个参数就是文件夹的路径，然而比它更为强大。</p>
<div style="font-size:18px;font-weight:bold;">步骤1. 第一个参数</div>
<p>第一个参数支持表达式。这就意味着你可以使用多个星号(*)来限制搜索特定的文件类型或者同时搜索多个文件夹。假设你有一个网站允许用户上传图片。每个用户在 &#8220;userimages&#8221; 目录下都有他/她自己单独的目录。在这些目录里面还有两个额外的目录，名叫 &#8220;HD&#8221; 和 &#8220;TN&#8221;，分别代表高清(完整大小)的图片和缩略图。想像一下你要遍历所有用户的 &#8220;TN&#8221; 目录并且打印出文件名。如果你使用 open_dir() 函数的话，代码量会很大；然而如果使用 glob()，那就简单多了。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userImages/*/TN/*'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Filename: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这将会搜索 userImages/any/TN/any 并将返回符合表达式的所有文件的列表。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test3<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test5<span style="color: #339933;">.</span>png
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>subfolder
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test2<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test4<span style="color: #339933;">.</span>gif
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username3<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>styles<span style="color: #339933;">.</span>css</pre></div></div>

<p>我们还可以进一步在 foreach 语句中限制文件类型：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userImages/*/TN/*.jpg'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Filename: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>现在，程序将只返回 Jpeg 图片。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test3<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test2<span style="color: #339933;">.</span>jpg</pre></div></div>

<p>这就更好了。如果你只需要 Jpeg 和 Gif 文件，不需要其它的，那该怎么办？又或者你只需要打印出目录名称？这就是第二个参数发挥作用的时候了。</p>
<div style="font-size:18px;font-weight:bold;">步骤2.第二个参数</div>
<p>正如前面所说到的那样，第二个参数是可选的。<br />
但它确实提供了一系列很棒的选项标志。这些选项允许你更改 glob() 的运作方式。<br />
<strong>GLOB_MARK</strong>: 给返回的每个目录添加斜杠<br />
<strong>GLOB_NOSORT</strong>: 按照目录中的形式返回文件(不排序)<br />
<strong>GLOB_NOCHECK</strong>: 如果没有符合条件的结果，则返回搜索表达式<br />
<strong>GLOB_NOESCAPE</strong>: 反斜杠不引用元字符<br />
<strong>GLOB_BRACE</strong>: 扩展 {a,b,c} 以匹配 ‘a’, ‘b’, 或 ‘c’<br />
<strong>GLOB_ONLYDIR</strong>: 只返回符合表达式的目录<br />
<strong>GLOB_ERR</strong>: 遇到读取错误(例如不可读的目录)时停止执行，默认情况下是忽略错误的</p>
<p>正如你所看到的那样，步骤1末尾的那个需求可以通过 GLOB_BRACE 来满足：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userImages/*/TN/{*.jpg,*.gif}'</span><span style="color: #339933;">,</span> GLOB_BRACE<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Filename: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>程序将返回以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username1<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test3<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test2<span style="color: #339933;">.</span>jpg
	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>test4<span style="color: #339933;">.</span>gif</pre></div></div>

<p>如果我们只想要输出子目录名称，我们可以使用 GLOB_ONLYDIR:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userImages/*/TN/*'</span><span style="color: #339933;">,</span> GLOB_ONLYDIR<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Filename: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$image</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>程序将输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	Filename<span style="color: #339933;">:</span> userImages<span style="color: #339933;">/</span>username2<span style="color: #339933;">/</span>TN<span style="color: #339933;">/</span>subfolder</pre></div></div>

<p>结论和额外的范例<br />
这个函数在PHP 4.3之后的版本中出现，但很奇怪的是，人们很少使用它。我也是最近才学会怎么使用它的。现在，我经常使用 glob() 来加载我的框架中的插件：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'includes/plugins/*.php'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$plugin</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$plugin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>就是这样，希望您能喜欢这个教程，谢谢。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/07/loop-through-folders-with-phps-glob.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[译]PHP面向对象入门</title>
		<link>http://www.yinzhili.com/2010/07/object-oriented-php-for-beginners.html</link>
		<comments>http://www.yinzhili.com/2010/07/object-oriented-php-for-beginners.html#comments</comments>
		<pubDate>Wed, 14 Jul 2010 07:51:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[译文]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[面向对象]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=265</guid>
		<description><![CDATA[<div style="color:#2d65a1;margin-bottom:10px;">原创译文，转载请注明出处。原作者为Jason Lengstorf，原文请 <a href="http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/" target="_blank">单击这里</a> 查看。</div>
<div>
对很多PHP程序员来说，面向对象编程是一个令人畏惧的概念，充满着复杂的语法以及其它障碍。正如我在我的《Pro PHP and jQuery》这本书中所详细描述的那样，您将学会面向对象编程(OOP)，这是一种[......]</div><p class='read-more'><a href='http://www.yinzhili.com/2010/07/object-oriented-php-for-beginners.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<div style="color:#2d65a1;margin-bottom:10px;">原创译文，转载请注明出处。原作者为Jason Lengstorf，原文请 <a href="http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/" target="_blank">单击这里</a> 查看。</div>
<div>
对很多PHP程序员来说，面向对象编程是一个令人畏惧的概念，充满着复杂的语法以及其它障碍。正如我在我的《Pro PHP and jQuery》这本书中所详细描述的那样，您将学会面向对象编程(OOP)，这是一种将相关动作组织在一起从而创造出更紧凑有效的代码的编程风格。</p>
<p style="font-size:18px;font-weight:bold;">理解面向对象编程</p>
<p>面向对象编程是一种允许开发者们将相似的多个任务组织到类里面的编程风格。这对于保持代码遵循“不要自我重复”(DRY,Don&#8217;t Repeat Yourself)这一准则以及保持易维护性是非常有帮助的。</p>
<p>DRY编程的主要好处是，如果一部分信息发生了变化，通常只需要更新一部分代码。开发者们最大的噩梦之一是维护那些数据被反复地声明的代码，这就意味着对程序的任何修改都将变成类似于&#8221;Where’s Waldo?&#8221;那样的令人无限沮丧的解谜游戏，因为数据和功能都是重复的。</p>
<p>OOP对于很多程序员来说是有点令人畏惧的，因为它引入了新的语法，而且乍一看去，比简单的面向过程或者内联的代码更为复杂。然而，仔细审视一番后你会发现，OOP是一种非常简洁并且极为简单的编程方法。</p>
<p style="font-size:14px;font-weight:bold;">理解对象和类</p>
<p>在深入了解OOP的细节之前，必须对类和对象之前的区别有一个大概的理解。本章节将讲解的是构建类的基础、它们的不同能力以及它们的一些用途。</p>
<p>你马上就会觉得OOP有点让你犯糊涂：经验丰富的开发者们在谈论对象和类，听起来似乎这二者是可以互换的。然而事实并非如此。二者的巨大区别可能会让你伤透脑筋。</p>
<p>例如，类，就像是房屋的设计图。它在纸上定义好了房屋的形状，各个部分之间的关系构造也都明确地设计好了，尽管这房屋事实上并不存在。</p>
<p>对象，就像是一栋真正的房屋，是根据设计图建造出来的。对象中存储的数据就像是建造房屋的木材、电线以及混凝土。</p>
<p>如果没有依照设计图组织起来，它们就只是一堆东西而已。但是当它们被组织到一起的时候，就成了一栋结构化的有用的房屋。</p>
<p>类，产生数据结构和相关操作，并使用这些信息来构建对象。同一时间内，同一个类可以被用来构建多个独立的对象。继续用我们刚才的那种建筑比喻，从一份设计图可以建造不同的房屋：它们外表看起来一样，但住在里面的人不同，里面的装修和家具也不同。</p>
<p style="font-size:14px;font-weight:bold;">类结构</p>
<p>创建类的语法是很简洁的：使用class关键字来声明一个类，然后给出类的名称，以及一对花括号({}):</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: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>创建类以后，使用new关键字，这个新的类就可以被实例化并存储到变量之中：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span></pre></div></div>

<p>要显示这个类的内容，使用var_dump()函数：</p>

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

<p>您可以试着将以上代码写到一个新的文件，命名为test.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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Class properties and methods go here</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>在您的浏览器中打开这个页面，地址是http://localhost/test.php，将会显示以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">object<span style="color: #009900;">&#40;</span>MyClass<span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">#1 (0) { }</span></pre></div></div>

<p>以这种最简单形式，您完成了您的第一个面向对象脚本。</p>
<p style="font-size:14px;font-weight:bold;">定义类的属性</p>
<p>为了给类添加数据，通常要使用属性或者类特定变量。它们就像常规的变量一样工作，区别就在于，它们被绑定到对象，因此就只能通过对象来访问。<br />
为了给MyClass添加属性，请添加以下代码：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这里的public关键字决定了这项属性的可见性，这在接下来的段落中会讲到。然后，使用标准的变量语法来命名这项属性，并且给变量赋值(尽管类的属性是不需要初始值的)。<br />
要读取这项属性并输出到浏览器，您要引用相应的对象才能读取这一属性的值：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span></pre></div></div>

<p>因为同一个类可能会生成多个实例，如果不引用单独的实例，程序将无法决定要读取哪一个对象的属性。箭头(->)是一种OOP结构，作用是访问给出的对象中所包含的属性和方法。<br />
参照下列代码来修改 test.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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Output the property</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>刷新您的浏览器，将显示以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!</span></pre></div></div>

<p style="font-size:14px;font-weight:bold;">定义类的方法</p>
<p>方法是指类特定的函数。类所能实现的单个操作是通过方法的形式在类中定义的。<br />
例如，要创建能够设置和修改类属性$prop1的方法，可以添加如下代码：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>注意——OOP允许对象通过$this来引用它们自己。在方法内部也同样是使用$this，但是在类的外部，您必须使用对象的名称。<br />
就像普通函数一样去调用这些方法，但是首先必须先引用它们所属的对象。通过以下代码您就可以实现读取MyClass的属性，修改其属性值，再读取出来：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the property value</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Set a new one</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Read it out again to show the change</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>刷新您的浏览器，您将看到以下输出结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">new</span> property value<span style="color: #339933;">!</span></pre></div></div>

<p>“当您使用一个类的多个实例时，OOP的好处就变得显而易见。”</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create two objects</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1 from both objects</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set new values for both objects</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I'm a new property value!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I belong to the second instance!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output both objects' $prop1 value</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>你刷新浏览器后应该就可以看到以下输出结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">I<span style="color: #0000ff;">'m a class property!
I'</span>m a <span style="color: #000000; font-weight: bold;">class</span> property<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a new property value!
I belong to the second instance!</span></pre></div></div>

<p> 正如您所看到的那样，OOP使对象成为单独的实体，这使得分离不同的代码段更加容易。</p>
<p style="font-size:18px;font-weight:bold;">OOP中的魔术方法</p>
<p>为了更方便地利用对象，PHP也提供了一系列的魔术方法，或者是一些在对象执行特定操作时所调用的特殊方法。这使得开发者们更容易地实现许多实用的任务。</p>
<p style="font-size:14px;font-weight:bold;">使用构造函数和析构函数</p>
<p>当一个对象被实例化时，通常需要立刻设置好一些东西。为了解决这一问题，PHP提供了魔术方法 __construct()，这个方法在创建新对象的时候会被自动调用。<br />
为了说明构造函数这一概念，我们给MyClass添加一个构造函数，并在创建新实例的时候输出一条消息：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>注意：__CLASS__ 返回的是所调用的类的名称。这就是人们所熟知的“魔术常量”。有很多魔术常量可供使用，您可以在PHP的手册中找到它们的资料。<br />
刷新浏览器您将看到以下输出结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.</span></pre></div></div>

<p>要在对象被销毁的时候调用一个函数，您可以使用 __destruct() 这个魔术方法。这对于类的清空(例如关闭数据库连接)是非常有用的。</p>
<p>在MyClass中定义魔术方法 __destruct() ，当对象被销毁时输出一条消息：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>定义了析构函数之后，程序将输出以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
End of file.
The class &quot;MyClass&quot; was destroyed.</span></pre></div></div>

<p>“当运行到文件末尾处的时候，PHP会自动释放所有资源。”</p>
<p>要显式地触发析构函数，您可以使用 unset() 函数来销毁对象：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the value of $prop1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>现在程序的输出结果应该是这样的：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></div></div>

<p style="font-size:14px;font-weight:bold;">转换为字符串</p>
<p>为了避免程序将MyClass当作字符串来输出而产生错误，另一个魔术方法 __toString() 就派上用场了。<br />
如果没有 __toString()，尝试将对象当作字符串来输出的行为将会导致出现致命错误。以下代码就是尝试用echo来输出对象，而不在适当的地方使用魔术方法：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这会产生以下输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
&nbsp;
&nbsp;
Catchable fatal error<span style="color: #339933;">:</span> Object of <span style="color: #000000; font-weight: bold;">class</span> MyClass could not be converted to string in  
<span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">40</span></pre></div></div>

<p>为了避免此项错误，添加一个 __toString() 方法：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy the object</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output a message at the end of the file</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;End of file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这样的话，尝试将对象转为字符串的时候将会调用 getProperty() 方法。刷新您的浏览器您将看到以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
Using the toString method<span style="color: #339933;">:</span> I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.
End of file.</span></pre></div></div>

<p>提示：除了本段所用到的几个魔术方法，还有更多的魔术方法可供使用，您可以在PHP手册中找到更多资料。</p>
<p style="font-size:18px;font-weight:bold;">使用类继承</p>
<p>通过extends 关键字，类可以继承其它类的方法和属性。例如，创建另一个类来继承MyClass并增加一个方法，您可以这么做：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>程序将输出以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></div></div>

<p style="font-size:14px;font-weight:bold;">重载继承的属性和方法</p>
<p>为了在新的类中修改现有的属性或者方法的行为，您只需在新的类中重新定义它们，即可实现重载：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p> 这样，程序将会输出：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></div></div>

<p style="font-size:14px;font-weight:bold;">重载时保留原有方法的功能</p>
<p>为了在继承的方法中添加功能的同时完整地保留住原始方法的功能，请使用 parent 关键字以及范围解析操作符(::)：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Call the parent class's constructor</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the object as a string</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">newMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这将会输出原始构造函数的内容以及新构造函数的内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
From a <span style="color: #000000; font-weight: bold;">new</span> method in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></div></div>

<p style="font-size:14px;font-weight:bold;">设置类的属性和方法的可见性</p>
<p>为了更好地控制对象，方法和属性都被设置了可见性。这样控制了属性和方法能从什么地方被访问。有以下三个可见性关键字：public, protected以及private。此外，一个方法或者属性能够被声明为 static (静态的)，这样，即便是类没有被实例化，它们也能被访问。</p>
<p>注意：可见性是PHP5的新特性。要了解关于PHP4的OOP兼容性的相关信息，请阅读PHP手册。</p>
<p style="font-size:12px;font-weight:bold;">受限的(protected)属性和方法</p>
<p>当一个属性或者方法被声明为protected，它就只能在该类自身或者其派生的子类中被访问。<br />
下面的代码将会把getProperty()方法声明为protected，并尝试从类的外部访问它：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Attempt to call a protected method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>运行这段程序的时候会输出以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to protected method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">''</span> in  
<span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">55</span></pre></div></div>

<p>现在，在MyOtherClass中定义一个新方法来调用 getProperty():</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Call the protected method from within a public method</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>程序将输出您想要的结果：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
I<span style="color: #0000ff;">'m a class property!
The class &quot;MyClass&quot; was destroyed.</span></pre></div></div>

<p style="font-size:12px;font-weight:bold;">私有的(private)属性和方法</p>
<p>如果一个方法或者属性被声明为private，那就只有定义它的那个类才能访问它。这就意味着，即便是另一个类继承了此类，那个类也不能够访问此类中的私有属性和方法。<br />
为了演示这一特性，以下代码将在MyClass中把getProperty()声明为private，然后尝试从MyOtherClass中调用 callProtected()：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new object</span>
<span style="color: #000088;">$newobj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyOtherClass<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Use a method from the parent class</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newobj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callProtected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>运行这段程序，则会显示报错信息：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0000ff;">&quot;MyClass&quot;</span> was initiated<span style="color: #339933;">!</span>
A <span style="color: #000000; font-weight: bold;">new</span> constructor in MyOtherClass<span style="color: #339933;">.</span>
&nbsp;
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">private</span> method MyClass<span style="color: #339933;">::</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from context <span style="color: #0000ff;">'MyOtherClass'</span> in  
<span style="color: #339933;">/</span>Applications<span style="color: #339933;">/</span>XAMPP<span style="color: #339933;">/</span>xamppfiles<span style="color: #339933;">/</span>htdocs<span style="color: #339933;">/</span>testing<span style="color: #339933;">/</span>test<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">49</span></pre></div></div>

<p style="font-size:12px;font-weight:bold;">静态的(static)属性和方法</p>
<p>如果一个属性或者方法被声明为static，那么不需要实例化这个类，它就能够被访问；你只需通过类的名称以及范围解析操作符以及属性或方法名就能访问它了。<br />
“使用静态属性的好处之一是，它们可以在整个脚本运行期间保存一些值不受影响。”<br />
为了演示这一属性，在MyClass中添加一个名为$count的静态属性以及一个名为plusOne()的静态方法。然后设置一个do&#8230;while循环，作用是在$count值小于10的时候输出递增的$count值。</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$prop1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm a class property!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was initiated!&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class &quot;'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot; was destroyed.&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Using the toString method: &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newval</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prop1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> plusOne<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: #0000ff;">&quot;The count is &quot;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">++</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyOtherClass <span style="color: #000000; font-weight: bold;">extends</span> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A new constructor in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;From a new method in &quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> callProtected<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">do</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Call plusOne without instantiating MyClass</span>
    <span style="color: #b1b100;">echo</span> MyClass<span style="color: #339933;">::</span><span style="color: #004000;">plusOne</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: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> MyClass<span style="color: #339933;">::</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>注意：访问静态属性的时候，美元符号($)必须在范围解析操作符(::)的后面。</p>
<p>当你运行这段程序的时候，输出的内容是这样的：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">The <span style="color: #990000;">count</span> is <span style="color:#800080;">1.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">2.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">3.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">4.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">5.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">6.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">7.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">8.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">9.</span>
The <span style="color: #990000;">count</span> is <span style="color:#800080;">10.</span></pre></div></div>

<p style="font-size:18px;font-weight:bold;">使用文档块(DocBlocks)来注释</p>
<p>尽管不是官方所设置的OOP的一部分，文档块注释风格是一种被广泛认可的类文档编写方式。它不仅为开发者提供了一个编写代码的标准，还被很多流行的软件开发包(SDK)所采纳，例如Eclipse和NetBeans，而且还被用来生成代码提示。<br />
文档块的使用方式就是增加了星号的块注释：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * This is a very basic DocBlock
 */</span></pre></div></div>

<p>文档块真正的好处在于使用tags(标签)，标签使用@符号作为开始，紧跟着的是标签的名字以及标签的值。文档块的标签允许开发者们定义文件的作者、类的授权许可、属性和方法的信息以及其它有用的信息。<br />
常用的标签如下：<br />
@author: 使用此标签来注明当前元素(可以是类、文件、方法或者一段代码)的作者。如果有多个作者，同一个文档块中可以使用多个author标签。作者名字的格式是 John Doe <john.doe@email.com>.<br />
@copyright: 此标签注明了当前元素版权的年份以及所有者。格式是 2010 Copyright Holder.<br />
@license: 此标签链接到当前元素的授权许可。格式是 http://www.example.com/path/to/license.txt 授权名称.<br />
@var: 此标签指定了变量或者类属性的类型和描述。格式是 type element description.<br />
@param: 此标签注明了函数或者方法的参数的类型和描述。格式是 type $element_name element description.<br />
@return: 此标签注明了函数或者方法的返回值的类型和描述。格式是 type return element description.</p>
<p>下面是一个使用了文档块注释的类的范例：</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>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A simple class
 *
 * This is the long description for this class,
 * which can span as many lines as needed. It is
 * not required, whereas the short description is
 * necessary.
 *
 * It can also span multiple paragraphs if the
 * description merits that much verbiage.
 *
 * @author Jason Lengstorf &lt;jason.lengstorf@ennuidesign.com&gt;
 * @copyright 2010 Ennui Design
 * @license http://www.php.net/license/3_01.txt PHP License 3.01
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> SimpleClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * A public variable
     *
     * @var string stores data for the class
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Sets $foo to a new value upon class instantiation
     *
     * @param string $val a value required for the class
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Multiplies two integers
     *
     * Accepts a pair of integers and returns the
     * product of the two.
     *
     * @param int $bat a number to be multiplied
     * @param int $baz a number to be multiplied
     * @return int the product of the two parameters
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bat</span><span style="color: #339933;">,</span> <span style="color: #000088;">$baz</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$bat</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$baz</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>一旦你仔细审视前面的这个类，你就会发现文档块的好处是显而易见的：一切都被清楚地定义，因此下一个开发者可以很快接手，而不需要费脑筋去思考太多。</p>
<p style="font-size:18px;font-weight:bold;">比较面向对象和面向过程的代码</p>
<p>事实上并没有一种真正正确或者错误的编程方法。<br />
话虽如此，本段落对软件开发(尤其是大型应用程序开发)中采用面向对象编程方法进行了有力的论证。</p>
<p style="font-size:14px;font-weight:bold;">理由1：易于实现</p>
<p>尽管起初有点令人畏惧，但事实上OOP提供了一种更容易处理数据的方法。因为对象可以在其内部存储数据，变量就不需要反复地从函数传递到函数了。<br />
而且，同一个类的多个实例可以同时存在，处理大量数据变得轻而易举。例如，想像一下，有两个人的信息需要在一个文件中处理。他们有名字、职业以及年龄。<br />
面向过程的方法<br />
下面是面向过程的处理方法：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'job'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Change the person's job</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">++</span><span style="color: #000088;">$person</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Add 1 to the person's age</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$person</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tom'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Button-Pusher'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'job'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Lever-Puller'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the starting values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 1: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 2: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Tom got a promotion and had a birthday</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Box-Mover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// John just had a birthday</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the new values for the people</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 1: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 2: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>执行后，程序输出下列内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color: #990000;">Array</span>   
<span style="color: #009900;">&#40;</span>   
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom   
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher   
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>   
<span style="color: #009900;">&#41;</span>   
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <span style="color: #990000;">Array</span>   
<span style="color: #009900;">&#40;</span>   
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John   
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller   
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>   
<span style="color: #009900;">&#41;</span>   
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color: #990000;">Array</span>   
<span style="color: #009900;">&#40;</span>   
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom   
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover   
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>   
<span style="color: #009900;">&#41;</span>   
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <span style="color: #990000;">Array</span>   
<span style="color: #009900;">&#40;</span>   
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John   
    <span style="color: #009900;">&#91;</span>job<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever<span style="color: #339933;">-</span>Puller   
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>   
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>尽管这段代码不是很糟糕，但是编码的时候还是要注意很多地方。人物属性数组必须从各个函数之间传递和返回，这就可能导致错误的发生。<br />
为了解决这个范例的问题，留给开发者的东西应当尽可能的少。只有当前操作的那些绝对必要的信息才传递给函数。</p>
<p>下面就轮到OOP来为您解决问题了。</p>
<p>面向对象的方法<br />
下面是针对此范例的面向对象方法：</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_job</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_age</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$job</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$job</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age <span style="color: #339933;">=</span> <span style="color: #000088;">$age</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> changeJob<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newjob</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_job <span style="color: #339933;">=</span> <span style="color: #000088;">$newjob</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> happyBirthday<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_age<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create two new people</span>
<span style="color: #000088;">$person1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Button-Pusher&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">34</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lever Puller&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">41</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output their starting point</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 1: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 2: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Give Tom a promotion and a birthday</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">changeJob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Box-Mover&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// John just gets a year older</span>
<span style="color: #000088;">$person2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">happyBirthday</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Output the ending values</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 1: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person1</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;Person 2: &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$person2</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;\/pre&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>这段程序会输出以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Button<span style="color: #339933;">-</span>Pusher
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">34</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">41</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Tom
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Box<span style="color: #339933;">-</span>Mover
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span>
<span style="color: #009900;">&#41;</span>
&nbsp;
Person <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> Person Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_name<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> John
    <span style="color: #009900;">&#91;</span>_job<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Lever Puller
    <span style="color: #009900;">&#91;</span>_age<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>要使这一方法面向对象，需要更多的步骤，但是一旦类被定义，添加和修改人物就变得轻而易举；人物的信息不需要从方法之间传递，只有那些绝对必不可少的信息才从方法之间传递。<br />
如果规模比较小，这种差别可能不明显，但是如果已经合理地实施了的话，一旦您的应用程序的大小增加，OOP将会显著地减少您的工作量。<br />
提示——并非所有东西都需要面向对象化。一个快速处理应用程序内部的少量数据的函数，并不需要写进类里面。根据您的需要来决定使用面向对象还是面向过程。</p>
<p style="font-size:14px;font-weight:bold;">理由2：更好的组织</p>
<p>OOP的另一个好处是，代码更容易被打包和分类。每个类通常都被写在单独的文件里面，而如果使用了统一的命名规范，访问这些类就是很简单的事情。<br />
假设您有个应用程序中包含了150个类，这些类通过根目录下的一个控制器文件来动态调用。所有150个类都遵循 class.classname.inc.php 这样的命名规范并保存在 inc 目录下。<br />
这个控制器能够实现PHP的  __autoload() 函数，作用是动态地调用某一时刻所需调用的类，而不是在这个控制器文件中把这150个文件都包含进来备用(或者是使用您自己实现的某种智能的方法来包含文件)：</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;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">'inc/class.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$class_name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.inc.php'</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>将每个类写到单独的文件中，这同样改善了代码的可移植性和可重用性。</p>
<p style="font-size:14px;font-weight:bold;">理由3：更容易维护</p>
<p>归功于面向对象的紧凑性，相对于冗长的面向过程的面条代码(spaghetti code)，对面向对象的代码的修改将会更容易实现。<br />
如果某个特定的信息数组获得了一个新的属性，面向过程的代码(最坏的情况下)可能需要在每个调用了此数组的函数中加上这一属性。<br />
而面向对象的代码很容易添加新的属性，只要修改那个处理这一属性的方法就可以了。</p>
<p>本章节所说到的很多好处都是OOP与DRY编程实践相结合的产物。创建易于维护的面向过程的代码是绝对有可能的，同样也可能写出很糟糕的面向对象的代码。[Pro PHP and jQuery]这本书将尝试展示一种新的编程方法，目的是将面向对象与良好的编码习惯相结合，编写出更干净的、容易阅读和维护的代码。
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/07/object-oriented-php-for-beginners.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用htaccess来增强Wordpress安全性</title>
		<link>http://www.yinzhili.com/2010/07/hardening-wordpress-with-htaccess.html</link>
		<comments>http://www.yinzhili.com/2010/07/hardening-wordpress-with-htaccess.html#comments</comments>
		<pubDate>Tue, 06 Jul 2010 06:47:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[译文]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[安全性]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=246</guid>
		<description><![CDATA[这是一篇原创译文，转载请注明出处，原文请单击<a href="http://blogsecurity.net/wordpress/article-210607" target="_blank">这里</a>查看。
本文将讨论一些有关于增强Wordpress博客安全性的安全技术，这些技术在那些托管的环境下尤为有效。 
首先我要说明的是本向导并不适合所有人，并且有可能影响一些第三方插件或者模板。 
情况还没有想象中的糟糕，如果你能够应用本向导中的知识，你就可[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/07/hardening-wordpress-with-htaccess.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p><span style="color: #3366ff;">这是一篇原创译文，转载请注明出处，原文请单击</span><a href="http://blogsecurity.net/wordpress/article-210607" target="_blank"><span style="color: #3366ff;">这里</span></a><span style="color: #3366ff;">查看。</span></p>
<p>本文将讨论一些有关于增强Wordpress博客安全性的安全技术，这些技术在那些托管的环境下尤为有效。 </p>
<p>首先我要说明的是本向导并不适合所有人，并且有可能影响一些第三方插件或者模板。 </p>
<p>情况还没有想象中的糟糕，如果你能够应用本向导中的知识，你就可以显著地增强你的博客的安全性。包括暴力攻击、插件枚举、目录列表、敏感信息泄露以及文件包含漏洞等类型的攻击都能被阻止。 </p>
<p>重要说明：请确保在操作之前备份好了您的Wordpress文件和数据库。 </p>
<p>步骤1 &#8211; 限制wp-content 和wp-includes</p>
<p>使用 .htaccess 中的 &lt;file&gt; 指令，我们可以限制访问除图像、样式表以及JavaScript之外的文件。这里的 .htaccess 文件看起来如下：</p>
<p>Order Allow,Deny<br />
Deny from all<br />
&lt;Files ~ &#8220;\.(css|jpe?g|png|gif|js)$&#8221;&gt;<br />
 Allow from all<br />
&lt;/Files&gt;</p>
<p>如果你想要允许特定的插件如Democracy被访问，我们可以将下面的指令追加到 wp-content/.htaccess 文件末尾：</p>
<p>&lt;Files &#8220;democracy.php&#8221;&gt;<br />
 Allow from all<br />
&lt;/Files&gt;</p>
<p>将以上内容保存到您的 wp-content 和 wp-includes 目录下的 .htaccess 文件中。补充说明一下，您还可以允许指定的某文件被访问，这样能使您的插件和/或模板正常工作，如果需要的话。这是一个更简洁的办法。</p>
<p>步骤2 &#8211; 限制访问 wp-admin</p>
<p>要限制访问 wp-admin，您有两种选择。选择以下方法中的一种，把包含相应指令的 .htaccess 文件放到您的 wp-admin 目录下。</p>
<p>您可以限制 IP:</p>
<p>order deny,allow<br />
allow from a.b.c.d # 这里的a.b.c.d就是您的静态 IP<br />
deny from all</p>
<p>以上代码将阻止那些IP地址不是 a.b.c.d 的访客通过浏览器访问此目录中的任何文件。这里的“a.b.c.d”应当与您的静态IP地址一致。</p>
<p>或者您还可以通过密码来限制：</p>
<p>AuthUserFile /etc/httpd/htpasswd<br />
AuthType Basic<br />
AuthName &#8220;restricted&#8221;<br />
Order Deny,Allow<br />
Deny from all<br />
Require valid-user<br />
Satisfy any</p>
<p>这里有一个bug。以上的规则代码会引发一个问题，如果那些在您的博客上发表评论的人没有输入邮箱地址，那么就会弹出一个输入密码的输入框。这是因为某些样式表和图片文件也位于wp-admin 目录之中。为了解决这一问题，我们可以将以上的代码包含于文件指令中，这个文件指令只会阻止 .php 文件被访问，不会阻止其它文件。这仍然能够阻止许多直接的攻击，并且同时还提供了许多额外的特性。下面是一个改进版：</p>
<p>&lt;Files ~ &#8220;\.(php)$&#8221;&gt;<br />
AuthUserFile /etc/httpd/htpasswd<br />
AuthType Basic<br />
AuthName &#8220;restricted&#8221;<br />
Order Deny,Allow<br />
Deny from all<br />
Require valid-user<br />
Satisfy any<br />
&lt;/Files&gt;</p>
<p>就是这样，有了这些措施，您的博客将更加安全。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/07/hardening-wordpress-with-htaccess.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>选择Web开发框架时需要考虑的15个重要因素</title>
		<link>http://www.yinzhili.com/2010/04/15-most-important-considerations-when-choosing-a-web-development-framework.html</link>
		<comments>http://www.yinzhili.com/2010/04/15-most-important-considerations-when-choosing-a-web-development-framework.html#comments</comments>
		<pubDate>Tue, 06 Apr 2010 02:37:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[译文]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[AVC]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[CURD]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[框架]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=200</guid>
		<description><![CDATA[<div style="color: red; font-weight: bold;">原创译文，未经许可不得转载。
原文地址：<a href="http://net.tutsplus.com/tutorials/other/15-most-important-considerations-when-choosing-a-web-development-framework/">http://net.tutsplus.com/tutorials/other/15-most-important-considerations-when-choosing-a-web-development-framework/</a></div>
    新的Web开发框架正[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/04/15-most-important-considerations-when-choosing-a-web-development-framework.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<div style="color: red; font-weight: bold;">原创译文，未经许可不得转载。<br />
原文地址：<a href="http://net.tutsplus.com/tutorials/other/15-most-important-considerations-when-choosing-a-web-development-framework/">http://net.tutsplus.com/tutorials/other/15-most-important-considerations-when-choosing-a-web-development-framework/</a></div>
<p>    新的Web开发框架正在以一种人们无法企及的速度涌现出来。在本文中，我们将会讨论你的下一个热门Web应用程序应该使用什么样的框架。<br />
    在如今这个时代，抢在您的竞争对手之前发布一个已完成的优雅的应用程序是至关重要的。从头开始编写代码(琐碎事情除外)是一件很费时的事，开发者要花很多时间去重新发明轮子，而这些时间如果用于开发新功能或完善代码会更好。<br />
    这就是Web开发框架存在的理由。它们往往已经包含了应用程序中所有的通用模块，包括数据库存取、权限控制、会话管理以及其它更多功能。<br />
    今天，我们将会讨论的是，你在选择一个框架之前所需要关心的各方面因素。有兴趣吗？那就马上开始。</p>
<h2>1. 使用环境</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/1.jpg"><img class="aligncenter size-full wp-image-202" title="图1" src="http://www.yinzhili.com/wp-content/uploads/2010/04/1.jpg" alt="" width="500" height="375" /></a><br />
    在你开始考察一个框架之前，你需要列出你的需求，看看这框架能否满足它们。<br />
    如果符合下面这些情形，你需要使用框架：<br />
        你的应用程序主要基于CRUD操作；<br />
        你需要将UI与底层逻辑合理地分开，但你没有时间去实现一个合适的系统；<br />
        你发现你为自己的Web应用程序所编写的程序库覆盖了用户授权、会话以及其它相关的常用功能；<br />
        你的老板希望你能在两天内为他开发一个CMS，而且你已经对框架有所了解；</p>
<p>如果符合下面这些情形，你就不需要框架：<br />
    你需要一个单独的漂亮的URL系统；<br />
    你只需要框架的某一特定部分，如ORM；<br />
    你的时间很紧，而且你必须从头开始学习框架；<br />
    有人告诉你框架能治疗癌症；</p>
<h2>2. 许<a href="http://www.yinzhili.com/wp-content/uploads/2010/04/2.jpg"></a>可证</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/2.jpg"><img class="aligncenter size-full wp-image-203" title="2" src="http://www.yinzhili.com/wp-content/uploads/2010/04/2.jpg" alt="" width="550" height="475" /></a></p>
<p>    在你开始使用框架进行开发之前，你得看一下这个框架是基于什么许可证发布的。尽管大部分许可证对于商业应用开发来说是自由的，但仍有一部分不那么自由。你最不希望看到的情况就是，你做完了整个应用程序后才发现，框架的许可证不允许你以商业形式发布代码。事先做好研究功课总好过事后的痛苦。<br />
    切记这不仅仅限制框架本身。你用于额外功能的插件或者扩展项都可能有一个隐藏的使用条款，所以你还得检查一下它的许可证才行。</p>
<h2>3. 软件模式</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/3.jpg"><img class="aligncenter size-full wp-image-204" title="3" src="http://www.yinzhili.com/wp-content/uploads/2010/04/3.jpg" alt="" width="500" height="400" /></a></p>
<p>    几乎所有的框架都无一例外地使用了MVC模式。MVC代表的是Model(模型)-View(视图)-Controller(控制器)，帮助你保存数据：模型，业务逻辑——控制器以及用户界面——视图，都是各自分开的。这些允许你编写更好更完善的代码，最终完成更好的应用程序。大家都用MVC并不代表你只需要知道这么多。还有其他不同的变种，包括MVP:Model(模型)-View(视图)-Presenter(呈现器), MVA: Model(模型)-View(视图)-Adapter(适配器) 以及 AVC: Application(应用程序)-View(视图)-Controller(控制器)。</p>
<h2>4. 主机需求</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/4.jpg"><img class="aligncenter size-full wp-image-205" title="4" src="http://www.yinzhili.com/wp-content/uploads/2010/04/4.jpg" alt="" width="600" height="396" /></a><br />
    作为Web开发者，我们可能更倾向于使用最尖端最顶级的平台，但是我们往往先要考虑到客户的需求以及预算的限制。使用独享主机来发布我们的应用程序往往会导致超出预算，因此我们不得不使用那些拥有正常模块和配置的共享主机。<br />
能很好兼容共享主机的框架包括：<br />
CodeIgniter<br />
CakePHP<br />
Kohana<br />
Zend Framework<br />
大多数其它 PHP框架</p>
<p>安装起来相对复杂的框架包括：</p>
<p>Ruby on Rails<br />
Django<br />
Pylons<br />
大多数非PHP框架</p>
<p>    事实上，你还是能够在共享主机上运行类似于Django的框架，前提是服务器上已经安装了必需的模块。你也可以在CGI模式下运行它，但速度会比原生模式慢很多。</p>
<h2>5. 安装容易程度</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/5.jpg"><img class="aligncenter size-full wp-image-206" title="5" src="http://www.yinzhili.com/wp-content/uploads/2010/04/5.jpg" alt="" width="575" height="322" /></a></p>
<p>    选择框架时，安装容易程度也是很重要的因素。一个框架，无论是重量级还是轻量级，如果使用者需要经过一系列繁琐步骤才能安装并且运行它，这就是问题了。<br />
    当应用程序已准备就绪并且测试完成，需要发布到生产服务器上的时候，这也会导致一个大问题。在这种情况下，一个安装和部署都很简单的框架就很有意义。<br />
    对大多数框架而言，安装过程和设置配置文件中的正确参数一样简单，然而对其它框架来说就是一件费时费力的事情。选择一个能让你尽快上手运行的框架。</p>
<h2>6. 学习曲线</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/6.jpg"><img class="aligncenter size-full wp-image-207" title="6" src="http://www.yinzhili.com/wp-content/uploads/2010/04/6.jpg" alt="" width="550" height="367" /></a></p>
<p>    每个框架都有它自己的小宇宙：命名规范、目录结构以及诸如此类的东西。某些框架在这方面非常灵活，而其它的就非常严格，例如在出现极小的错误时要抛出错误都非常繁琐。<br />
    某些框架在实现一个功能的时候会遵循一个规范，而其它的就各行其是。<br />
    在选择框架的时候，记住要选择学习曲线最平缓的那个。如果你不知道这框架是用什么语言编写的，那就必须把学习这语言本身的学习曲线也考虑在内。我曾见过不少从CakePHP转向Django的开发者，他们不得不同时学习Python和Django，因此他们纷纷表示压力很大。<br />
    如果你需要同时学习框架和它所使用的语言，你就得悠着点了。</p>
<h2>7. 代码库</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/7.jpg"><img class="aligncenter size-full wp-image-208" title="7" src="http://www.yinzhili.com/wp-content/uploads/2010/04/7.jpg" alt="" width="530" height="282" /></a></p>
<p>让我们面对这样一个现实，那就是，人们接受一个框架是因为它的核心库。库必须能让你从编写重复代码的过程中解脱出来，同时也能允许你自行增加更多的功能和特性。<br />
大多数框架提供了以下大部分功能的类库：<br />
AJAX<br />
身份验证<br />
授权<br />
缓存<br />
数据清理<br />
数据验证<br />
模板<br />
URL 映射和重写</p>
<p>    当然，并不是人人都需要一个包罗万象的框架。很多人更希望框架能处理小部分功能而让开发者来处理剩下的那部分。在这些情况下，你需要确保你所考虑的框架是否只拥有你所需要的特性。<br />
    目前框架中流行的趋势是以类库的形式创建框架。换句话说，它允许你根据需要来对库进行替换。在这方面的绝佳典范就是Pylons。它甚至允许你替换它的大多数部分，从ORM到模板语言都行。相对于那些紧耦合的框架，人们喜欢这种松耦合的框架。</p>
<h2>8. 数据库抽象以及ORM</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/8.jpg"><img class="aligncenter size-full wp-image-209" title="8" src="http://www.yinzhili.com/wp-content/uploads/2010/04/8.jpg" alt="" width="362" height="438" /></a></p>
<p>    几乎所有应用程序都要存取数据库。如论如何，你都要在开发整个应用程序的过程中这么做，大多数框架都提供了数据库存取类给你使用。因此，当你在选择应用程序的时候，选择能使你的应用程序变得与数据库无关的那一个。万一你要切换数据库，而如果你的框架为你做了这些工作的话，你永远都不用在数据库那部分操心。<br />
    你所要考虑的第二部分就是框架的ORM功能。不需太多技巧，ORM或者Object Relational Mapping(对象关系映射)允许你将数据以对象的形式表现出来，并且将它与其它对象关联起来。如果你想的话，想象一下一个能够获取信息的对象数据库。<br />
包含了ORM特性的框架包括：CakePHP、Django以及Ruby。使用类似Pylons的框架的时候你甚至可以用你自己选择的ORM。</p>
<h2>9. 内置的JS库</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/9.jpg"><img class="aligncenter size-full wp-image-210" title="9" src="http://www.yinzhili.com/wp-content/uploads/2010/04/9.jpg" alt="" width="475" height="396" /></a></p>
<p>    争论的另一点就是捆绑的JavaScript库。大多数允许你轻松地切换，然而框架内置的AJAX方法大多数情况下仍然只针对某一特定的JS库。这种潜规则意味着你将不得不手动去开发那一功能。<br />
    另一方面，提供了通用方法的那些框架对你来说的好处是，你可以轻而易举地切换你要使用的JavaScript库。<br />
仅供参考：CakePHP和Ruby on Rails能够以标准形式在Prototype和Scriptaculous之间切换。</p>
<h2>10. 单元测试</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/10.jpg"><img class="aligncenter size-full wp-image-211" title="10" src="http://www.yinzhili.com/wp-content/uploads/2010/04/10.jpg" alt="" width="308" height="502" /></a></p>
<p>    我就是那种极其依赖于单元测试的开发者。维基百科上对单元测试的定义是这样的：<br />
单元测试是一种软件检验测试方法，程序员可以通过它来测试源代码中的单个部分是否符合要求。一个单元就是应用程序中最小的可测试的部分。<br />
    在这种情况下，能让我编写单元测试的框架对我很有益处。很多框架，如CodeIgniter、CakePHP还有Zend都允许你创建自定义的测试，作为对核心测试的补充，目的是对应用程序的重要部分进行检查。</p>
<h2>11. 可伸缩性</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/11.jpg"><img class="aligncenter size-full wp-image-212" title="11" src="http://www.yinzhili.com/wp-content/uploads/2010/04/11.jpg" alt="" width="550" height="388" /></a></p>
<p>    一般的Web开发者不必考虑框架的可伸缩性问题。通常情况下I/O和网络延迟比框架的可伸缩性更为重要。甚至Twitter那神话般的可伸缩性问题都不是所考虑的框架本身的过错。如果有人要求你放弃一个框架并指出它的伸缩问题，你可以笑而不语。伸缩问题的原因很少由框架产生。当然你也可以对代码稍作优化，但是通常情况下伸缩问题的主要原因在别的方面。</p>
<h2>12. 文档</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/12.jpg"><img class="aligncenter size-full wp-image-213" title="12" src="http://www.yinzhili.com/wp-content/uploads/2010/04/12.jpg" alt="" width="575" height="309" /></a><br />
    框架的文档往往是它能否成功的关键。解释详尽的文档能够吸引更多用户和爱好者。质量差的、令人费解的文档会使人们感到迷惑并且惹恼他们，使他们放弃该框架。<br />
    寻找一个文档中包含有丰富的范例、实例代码、文章以及教程的框架。</p>
<h2>13. 社区</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/13.jpg"><img class="aligncenter size-full wp-image-214" title="13" src="http://www.yinzhili.com/wp-content/uploads/2010/04/13.jpg" alt="" width="464" height="354" /></a></p>
<p>    尽管有合适的文档，但你不可避免地还是会遇到需要解决的问题，因此你需要去框架的支持社区寻求帮助。我个人已经在很多个社区进行过交流，其中一个社区的成员尖酸刻薄地对新手进行嘲笑，然而另一个社区的成员们却十分热心地为新手提供力所能及的帮助。因此对于最终选择的是哪个框架开始工作的这一问题，我就不做解释了。<br />
    如果一直如此，支持社区将会成就或者毁掉一个框架。社区成员表现得这样急功近利的话，你将会对框架心生怨恨，而不是那些人。社区成员表现得彬彬有礼的话，你就会更倾向于选择那个框架。如果框架拥有友好的社区，能够为新开发者提供帮助的话，那就选择它。</p>
<h2>14. Bug 修复/更新</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/14.jpg"><img class="aligncenter size-full wp-image-215" title="14" src="http://www.yinzhili.com/wp-content/uploads/2010/04/14.jpg" alt="" width="550" height="344" /></a></p>
<p>    Web开发者们不愿意自己编写框架的原因之一是他们将要独自完成bug的修复以及更新工作。而对于一个大的框架，每天都有成千上万的开发者对代码进行审视。一旦发现有bug，就能立即得到修复。<br />
    选择一个不那么死气沉沉的框架。你不希望有黑客来告诉你框架有个安全漏洞可以被用来入侵你的网站吧。相对而言，你更希望由框架的开发者们给你提供一个补丁来修正问题。 选择一个更新频繁的框架，更重要的是该框架是开放性的，大家都能发现bugs和尽快修复它们。</p>
<h2>15. 创建扩展的容易程度以及可用性</h2>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/04/15.jpg"><img class="aligncenter size-full wp-image-216" title="15" src="http://www.yinzhili.com/wp-content/uploads/2010/04/15.jpg" alt="" width="564" height="339" /></a></p>
<p>    尽管框架覆盖了一个应用程序的重要基础部分，但是你还需要编写一些代码。尽量使它变得通用，这样你就可以在你的其它应用程序中对它进行重用，甚至是公开发布给人们使用。<br />
    选择一个能够让你轻松进行扩展的框架。以CakePHP为例，扩展一个控制器需要顾及到各个组件以及由辅助函数和视图。无论是那种情况，创建一个扩展就像定义一个继承于父类的子类一样简单。<br />
    选择框架的时候，记住要考虑到插件的可用性。通常情况下你没有时间从头开始创建一个自定义扩展。拥有一个巨大的扩展库可供选择，这就在很大程度上解决了这一问题。扩展的质量比数量更重要。</p>
<h2>结论</h2>
<p>    现在我们已经讨论了你在选择框架之前所应该考虑的各个方面，包含从框架是否满足需要到bug修复和更新在内的所有内容。但愿这对你有所帮助，也希望你能觉得有趣。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/04/15-most-important-considerations-when-choosing-a-web-development-framework.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>100条超级残暴的英文翻译</title>
		<link>http://www.yinzhili.com/2009/12/100-brilliant-translations.html</link>
		<comments>http://www.yinzhili.com/2009/12/100-brilliant-translations.html#comments</comments>
		<pubDate>Fri, 11 Dec 2009 07:06:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[译文]]></category>
		<category><![CDATA[翻译]]></category>
		<category><![CDATA[英文]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=143</guid>
		<description><![CDATA[1、Do not argue with an idiot. He will drag you down to his level and beat you with experience.
你永远不能战胜一个纯SB,因为他会把你的智商拉到跟他个水平,然后用丰富的经验打败你
2、I asked God[......]<p class='read-more'><a href='http://www.yinzhili.com/2009/12/100-brilliant-translations.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>1、Do not argue with an idiot. He will drag you down to his level and beat you with experience.<br />
你永远不能战胜一个纯SB,因为他会把你的智商拉到跟他个水平,然后用丰富的经验打败你</p>
<p>2、I asked God for a bike, but I know God doesn’t work that way. So I stole a bike and asked for forgiveness.<br />
开始我直接求上帝赐辆自行车。 后来我琢磨上帝办事儿不是这个路数。 于是老子偷了一辆然后求上帝宽恕。</p>
<p>3、I want to die peacefully in my sleep, like my grandfather.. Not screaming and yelling like the passengers in his car.<br />
我希望能像爷爷那样，安静地在睡梦中死去…… 而不是要像他开的车上那些惨叫滴乘客一样死法啊！</p>
<p>4、If 4 out of 5 people SUFFER from diarrhea… does that mean that one enjoys it?<br />
如果4/5的人在忍受腹泻的痛苦，那剩下1/5咋回事？很享受吗？</p>
<p>5、We live in a society where pizza gets to your house before the police.<br />
在咱们这噶哒送外卖的都比警察来的快.</p>
<p>6、I thought I wanted a career, turns out I just wanted paychecks.<br />
曾以为我想要的是职业，结果发现我只是想要工资。</p>
<p>7、Light travels faster than sound. This is why some people appear bright until you hear them speak.<br />
光总是比声音跑的快点….这就是为嘛有些SB只有在开口说话之后你才发现他是在装B…</p>
<p>8、Remember, if you smoke after sex you’re doing it too fast.<br />
记着，如果你在嘿咻后冒烟了，证明你整得太快了。</p>
<p>9、I saw a woman wearing a sweat shirt with “Guess” on it…so I said “Implants?”<br />
瞅见个姐姐，胸前衣服上写着“Guess”…俺就问了一句：“…隆过？”</p>
<p>10、The last thing I want to do is hurt you. But it’s still on the list.<br />
直译：在这个世界上,我最不愿意做的事就是伤害你,但是这件事仍在我的考虑之列。<br />
意译a：我真不想伤害你，但你也别逼我。<br />
意译b：吾虽不杀伯仁，伯仁由我而死。</p>
<p>11、If sex is a pain in the ass, then you’re doing it wrong…<br />
a. 如觉嘿咻乃屁眼不能承受之痛，那是你操错洞…<br />
b. 若XXOO是下体的痛，那么，是你操错。</p>
<p>12、The early bird might get the worm, but the second mouse gets the cheese.<br />
a. 早起滴小鸟有虫虫！晚到的老鼠有奶酪！<br />
b. 早起的鸟儿有虫吃，早起的虫儿被鸟吃。</p>
<p>13、Having sex is like playing bridge. If you don’t have a good partner, you’d better have a good hand.<br />
XXOO就象打桥牌。 如果对手不好使，自己的手必须好使。</p>
<p>14、 Some people are like Slinkies … not really good for anything, but you can’t help smiling when you see one tumble down the stairs.<br />
有些人就像 Slinkies （弹簧玩具），没什么实在用处，但看他们在楼梯上倒腾来捣腾去还是很有喜感。</p>
<p>15、Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason.<br />
政客和纸尿布有一个共同点就是：他们都很有规律地被替换，而且因为同一个理由——脏了！！</p>
<p>16、War does not determine who is right – only who is left.<br />
战争不能决出正义，但能判出哪方出局。</p>
<p>17、Women might be able to fake orgasms. But men can fake a whole relationship.<br />
a. 女人的高潮可能是装出来的。但是男人TMD可以整段感情都是装出来的！<br />
b. 女人假装高潮以获取真实的感情；男人假装感情以获取真实的高潮。</p>
<p>18、We never really grow up, we only learn how to act in public.<br />
我们永远不可能真正的成熟，我们只是学会在众人面前装逼。</p>
<p>19、Men have two emotions: Hungry and Horny. If you see him without an erection, make him a sandwich.<br />
男人就两种状态：饿 和 性饥渴。 要是他不硬，就给他个三明治！</p>
<p>20、My mother never saw the irony in calling me a son-of-a-bitch.<br />
我妈每次对着我骂草泥马的时候都没看出其中笑点。</p>
<p>21、If you think nobody cares if you’re alive, try missing a couple of payments.<br />
你要是觉得没人在乎你的死活,那你不妨尝试一下跟你的债主玩躲猫猫~~</p>
<p>22、Sex is not the answer. Sex is the question. “Yes” is the answer.<br />
XXOO并不是结论而是个问题…爽不爽才是答案…</p>
<p>23、Evening news is where they begin with ‘Good evening’, and then proceed to tell you why it isn’t.<br />
晚间新闻总是以“晚上好”开头，再告诉你你为什么好不了。</p>
<p>24、How is it one careless match can start a forest fire, but it takes a whole box to start a campfire?<br />
直译：一根火柴能点着整片森林，一盒火柴也生不起个营火，这咋回事！<br />
意译：想当年哥戴套都能让人怀孕，看今朝叔竟然去看男科</p>
<p>25、Knowledge is knowing a tomato is a fruit; Wisdom is not putting it in a fruit salad.<br />
直译：知识就是说你知道西红柿是一种水果；智慧就好似不要把它放进水果沙拉里。<br />
意译a：知识就是告诉你说应该把鸡蛋放进篮子，智慧则是叫你不要把所有鸡蛋都放进一个篮子。<br />
意译b:所谓知识就是知道韩少和小四都属于80后，但智慧告诉你这终还是男女有别~</p>
<p>26、If God is watching us, the least we can do is be entertaining.<br />
上帝瞅着咱们呢，大伙好歹喜感点吧！</p>
<p>27、Never, under any circumstances, take a sleeping pill and a laxative on the same night.<br />
无论，在任何情况下，永远，不要在一个夜晚，同时吃，安眠药，和通便灵。</p>
<p>28、I didn’t fight my way to the top of the food chain to be a vegetarian<br />
老子拼死拼活奋斗到食物链顶端，不是为了成为一个素食者。</p>
<p>29、A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station..<br />
公车站呀公车停。火车站呀火车停。俺桌上有个工作站…</p>
<p>30、If I agreed with you we’d both be wrong.<br />
要是我听你的，咱俩就都2B了。</p>
<p>31、 Did you know that dolphins are so smart that within a few weeks of captivity, they can train people to stand on the very edge of the pool and throw them fish?<br />
海豚可聪明了你知道不？只要驯养几个星期，它们就能让人类乖乖站在池边给它们扔鱼吃了。</p>
<p>32、A computer once beat me at chess, but it was no match for me at kick boxing.<br />
a. 下棋，我不行；玩跆拳道，电脑不行！<br />
b. 下象棋电脑把我玩得团团转，拳击我能把机箱踹得七零八散！</p>
<p>33、When tempted to fight fire with fire, remember that the Fire Department usually uses water.<br />
以暴治暴，不如以抱治爆。</p>
<p>34、 Children: You spend the first 2 years of their life teaching them to walk and talk. Then you spend the next 16 years telling them to sit down and shut-up.<br />
孩子就是：你先花2年，教丫们走路和说话。然后你再花16年教丫们坐定和闭嘴。</p>
<p>35、Why does someone believe you when you say there are four billion stars, but check when you say the paint is wet?<br />
为什么当你说天上有400亿星星时他不怀疑，却偏要检查你所说的“油漆未干”？</p>
<p>36、Better to remain silent and be thought a fool, than to speak and remove all doubt.<br />
a. 宁愿闭嘴当傻瓜，也别学乌鸦乱呱呱。<br />
b. 越解释越SB，不说话最NB。<br />
c. 剽悍的人生不需要解释<br />
d. 宁可闭口被人当SB，也不张口解释所有疑。</p>
<p>37、A bank is a place that will lend you money, if you can prove that you don’t need it.<br />
银行就是当你证明了你不需要钱的时候可以借钱给你的地方</p>
<p>38、Laugh at your problems, everybody else does.<br />
a.对你的问题哈哈大笑吧，别人都在这么做。<br />
b. 你有什么不开心的？说出来给大家开心开心。</p>
<p>39、The voices in my head may not be real, but they have some good ideas!<br />
a. 我知道没人在我脑子里跟我聊天，但那些话真TM有用！<br />
b. 我幻想的低吟不一定是真的，但足够让我意淫</p>
<p>40、A clear conscience is usually the sign of a bad memory.<br />
a. 无愧于心哈？记性不好吧？<br />
意译b： 自从那次在人妖身边醒来，每次去夜店我都提醒自己“一定要戴眼镜……”<br />
c. 意识清醒了，意味着不堪回首了。</p>
<p>41、Good girls are bad girls that never get caught.<br />
a. 所谓的好姑娘，咳！就是还没被群众抓到的JP女·····<br />
b. 想立牌坊就得会装</p>
<p>42、He who smiles in a crisis has found someone to blame.<br />
临危忽然微笑的那谁，定是找到替罪羊鸟~</p>
<p>43、Women will never be equal to men until they can walk down the street with a bald head and a beer gut, and still think they are sexy.<br />
如果女人能做到以秃顶和啤酒肚在大街上晃还觉得自己倍儿性感——此时估计男女能平等。</p>
<p>44、The shinbone is a device for finding furniture in a dark room.<br />
小腿上的骨头——在黑房间里找准家具位置的好装备。</p>
<p>45、The main reason Santa is so jolly is because he knows where all the bad girls live.<br />
圣诞老人当然美，他知道所有YD妞住哪儿啊！</p>
<p>46、To steal ideas from one person is plagiarism. To steal from many is research.<br />
剽窃一个人的叫剽窃，剽窃许多人的叫研究。<br />
意译：窃钩者诛，窃国者为诸侯。</p>
<p>47、Some cause happiness wherever they go. Others whenever they go.<br />
有些人一来大家就开心了；有些人一走大家就开心了。.</p>
<p>48、I discovered I scream the same way whether I’m about to be devoured by a great white shark or if a piece of seaweed touches my foot.<br />
我发现，我滴脚丫被一小片儿海藻擦过时，我滴那个惨叫声——和我被大白鲨吓坏时的惨叫声是一样滴。</p>
<p>49、Crowded elevators smell different to midgets.<br />
直译：你若是挤满人的电梯里的小矮人，肯定能嗅出不同的味儿。<br />
意译：灵感来自于所站的角度与众不同。</p>
<p>50、I didn’t say it was your fault, I said I was blaming you.<br />
我可没怨你！我是在谴责你！</p>
<p>51、Whenever I fill out an application, in the part that says “If an emergency, notify:” I put “DOCTOR”. What’s my mother going to do?<br />
当我填表的时候，有一项是“紧急情况联系：” 我填上了“医生”，到时候我妈能帮上什么忙？！</p>
<p>52、God must love stupid people. He made SO many.<br />
上帝一定倍儿爱SB，不然他造这么多！！！！！！！</p>
<p>53、Behind every successful man is his woman. Behind the fall of a successful man is usually another woman.<br />
每一个成功的男人背后都有一个女人。每一个翻了船的成功男人背后往往是另一个女人。</p>
<p>54、I always take life with a grain of salt, …plus a slice of lemon, …and a shot of tequila.<br />
生活对于我来说是小菜一碟，我通常就着盐和柠檬，再来一小杯烈性酒。<br />
意译a：生活，是一团麻……绳，……再加一根蜡烛……一柄皮鞭。<br />
意译b: 生活，不就是先狂吃菜，然后把烈性白酒一饮而尽？</p>
<p>55、The sole purpose of a child’s middle name, is so he can tell when he’s really in trouble.<br />
直译：小孩子要中间名，纯粹是为了让他知道他啥时候真的有麻烦了。<br />
直译a：起个全名就为了揍孩子前可以底气十足地喊出来。<br />
意译b：贾君鹏这名字就为了让他妈喊他回家吃饭！</p>
<p>56、It’s not the fall that kills you; it’s the sudden stop at the end.<br />
跳楼的时候，“啊——”的时候还没死，“啪！”那才是死了。</p>
<p>57、Artificial intelligence is no match for natural stupidity.<br />
人工智能从来敌不过天然请勿使用不良词语。</p>
<p>58、Never hit a man with glasses. Hit him with a baseball bat.<br />
直译：不要用眼镜腿来打男人，用棒球击杆。<br />
意译: 要下手就得狠，甭来毛毛雨。</p>
<p>59、There’s a fine line between cuddling and holding someone down so they can’t get away.<br />
直译：要想留住谁，在抱摔的时候有一条细线区分出你是否为高手。<br />
b：抱摔是留不住女人的，搂抱才管用。/抱和爆是有区别的。 /推倒和拥抱是有微妙的区别的哟！/拥抱和柔道里的压制是有区别的！</p>
<p>60、A bargain is something you don’t need at a price you can’t resist.<br />
所谓砍价，就是这东西虽然你不需要，但价格太好必须要买下来！</p>
<p>61、Never get into fights with ugly people, they have nothing to lose.<br />
绝对不要和长得丑的争执，他们已经没什么可输的了。</p>
<p>62、You do not need a parachute to skydive. You only need a parachute to skydive twice.<br />
过了河也别拆桥，没准你还要回来呢。</p>
<p>63、The difference between an oral thermometer and a rectal thermometer is in the taste.<br />
口腔体温计和菊花体温计有啥不同？尝尝看就知道了</p>
<p>64、My opinions may have changed, but not the fact that I am right.<br />
我的观点或许改变了，但我是正确的这一事实却亘古不变。<br />
意译：我们要搞共产主义，也要搞有中国特色的社会主义。</p>
<p>65、My psychiatrist told me I was crazy and I said I want a second opinion. He said okay, you’re ugly too.<br />
心理医师：你神经病！！！ 我：能说点别的吗？<br />
心理医师：好！！而且你真TM丑！！！</p>
<p>66、A little boy asked his father, “Daddy, how much does it cost to get married?” Father replied, “I don’t know son, I’m still paying.”<br />
一单纯傻儿子问他爹：“爸，结婚到底要花多少钱啊？”他爸说：“儿啊，我真不知道…没看见我还在交钱吗？”</p>
<p>67、 Some people say “If you can’t beat them, join them”. I say “If you can’t beat them, beat them”, because they will be expecting you to join them, so you will have the element of surprise.<br />
有人说“打不过，就合作”。我说“打不过，也要打”。因为丫们指望你加入，得来点惊喜~！</p>
<p>68、When in doubt, mumble.<br />
脑子不好使的话，你就嘟囔。<br />
不明白的话，哼哼试试。</p>
<p>69、I intend to live forever. So far, so good.<br />
我已决定长生不老，目前为止，感觉良好。</p>
<p>70、Hospitality: making your guests feel like they’re at home, even if you wish they were<br />
好客就是：让客人觉得他们像在他们家一样，尽管你真的希望他们滚回他们家。</p>
<p>71、If at first you don’t succeed, skydiving is not for you!<br />
跳伞是这样一种运动：不成功则成仁！</p>
<p>72、If you are supposed to learn from your mistakes, why do some people have more than one child.<br />
假设我们会吃一堑，长一智，为什么总有人生了一个还会生？</p>
<p>73、A TV can insult your intelligence, but nothing rubs it in like a computer.<br />
电视侮辱你的智商，电脑则是吐你槽的终极存在！</p>
<p>74、Knowledge is power, and power corrupts. So study hard and be evil.<br />
知识就是力量，力量会邪恶化。那么就好好学习当大魔王吧！</p>
<p>75、Money can’t buy happiness, but it sure makes misery easier to live with.<br />
钱买不来幸福，但有了它，痛苦的日子会好熬一点。</p>
<p>76、Always borrow money from a pessimist. He won’t expect it back.<br />
跟悲观主义者借钱吧！他反正不指望你还！！！</p>
<p>77、Worrying works! 90% of the things I worry about never happen.<br />
担忧真的好使！！老子担心的事儿90%都没发生！！</p>
<p>78、Virginity is like a soapbubble, one prick and it is gone.<br />
贞操如泡影，一戳无踪影。</p>
<p>79、Nostalgia isn’t what it used to be.<br />
这年头，连怀旧都不如从前够味儿了。</p>
<p>80、With sufficient thrust, pigs fly just fine.<br />
只要主义真，猪也成超人。<br />
信春哥 ，得永生</p>
<p>81、A bus is a vehicle that runs twice as fast when you are after it as when you are in it.<br />
公车总在被追赶的时候的速度要比你在里面时的速度快两倍。<br />
一旦你在公车后面追，它的速度就会变得比你在里面所感觉得快。</p>
<p>82、I should’ve known it wasn’t going to work out between my ex-wife and me. After all, I’m a Libra and she’s a bitch.<br />
我早就该知道我跟我EX没戏！归根结底，我是天枰她是JP！</p>
<p>83、Hallmark Card: “I’m so miserable without you, it’s almost like you’re still here.”<br />
豪马克卡上的话：没有你我痛苦万分，正如你就在此处。（没有女人冷冷清清，有了女人鸡犬不宁）</p>
<p>84、You’re never too old to learn something stupid.<br />
越活越2~ /活到老,2到老</p>
<p>85、A diplomat is someone who can tell you to go to hell in such a way that you will look forward to the trip.<br />
直译：外交家们说让你下地狱的时候，措辞也好像你正巴不得来这么趟旅行。</p>
<p>86、 I got in a fight one time with a really big guy, and he said, “I’m going to mop the floor with your face.” I said, “You’ll be sorry.” He said, “Oh, yeah? Why?” I said, “Well, you won’t be able to get into the corners very well.”<br />
我和个壮汉闹急了。 他：老子非得用你丫脸把地给擦了！！！ 我：你会后悔的！！ 他：噢？真哒？怎么讲？ 我：呃，边边角角的地方你擦不到！！！</p>
<p>87、Some people hear voices.. Some see invisible people.. Others have no imagination whatsoever.<br />
有些人吹牛说丫能通灵，有些人吹牛说丫有阴阳眼，其他人只是没有这种想象力而已</p>
<p>88、I like work. It fascinates me. I sit and look at it for hours.<br />
啊我好爱好爱工作啊~ 工作让我好着迷啊~我TMD死盯着它几个小时了啊！</p>
<p>89、We have enough gun control. What we need is idiot control.<br />
枪支管理不是问题，SB管理才是问题</p>
<p>90、Women may not hit harder, but they hit lower.<br />
女人或许击得并不重，但她们击得更低…..<br />
女人总是能击中男人的要害。</p>
<p>91、Just remember…if the world didn’t suck, we’d all fall off.<br />
直译：记着吧……世界要不恶心，我们早被吐掉了。<br />
别抱怨了，这个世界要是真和谐了，我们这种人就不应该存在~<br />
意译：你们TMD给我记住：没有和谐社会，你们早让美帝国主义给糟蹋了！</p>
<p>92、Jesus loves you, but everyone else thinks you’re an asshole.<br />
上帝蜀黍疼你，只是大家都觉得你2B而已</p>
<p>93、I used to be indecisive. Now I’m not sure.<br />
a. 过去老子左右为难。现在老子优柔寡断。<br />
b. 我曾是个怀疑论者，现在我很怀疑这一点。<br />
c. 过去我难以决断，现在我不大确信是否还是如此。</p>
<p>94、I don’t trust anything that bleeds for five days and doesn’t die.<br />
老子打死都不信流血5天还不挂的物种。/老子打死都不信娘们儿。</p>
<p>95、If you keep your feet firmly on the ground, you’ll have trouble putting on your pants.<br />
直译：如果你始终脚踏实地，那就别想穿裤子了。<br />
意译：人太老实没法活。</p>
<p>96、To be sure of hitting the target, shoot first and call whatever you hit the target.<br />
别人是指哪儿打哪儿，我是打哪儿指哪儿。</p>
<p>97、You are such a good friend that if we were on a sinking ship together and there was only one life jacket… I’d miss you heaps and think of you often.<br />
好吧，既然这艘船要沉了，救生衣又只有一件，我就勉为其难的虚伪一下，你对我来说实在是不能再好的朋友了…我一定会经常想念你滴~!!!</p>
<p>98、Going to church doesn’t make you a Christian any more than standing in a garage makes you a car.<br />
站在车库的你并不会变成一辆车是吧？所以站在教堂的你也不会变成基督徒。</p>
<p>99、Change is inevitable, except from a vending machine.<br />
世界总是在变，但我却怎么也便不出来。<br />
意译：人生何处不杯具，唯有面对饮水机。</p>
<p>100、Whoever coined the phrase “Quiet as a mouse” has never stepped on one.<br />
发明“鼠动无声”这词儿的哥们一定没踩上过一只。</p>
<p>/***************************************************************<br />
以上内容转载自http://www.wuliaoo.com/can-bao-ying-wen-fan-yi.html，版权属原始作者所有。<br />
****************************************************************/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2009/12/100-brilliant-translations.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[译文]在NetBeans IDE中增加CodeIgniter代码提示</title>
		<link>http://www.yinzhili.com/2009/08/using-code-completion-for-codeigniter-in-netbeans.html</link>
		<comments>http://www.yinzhili.com/2009/08/using-code-completion-for-codeigniter-in-netbeans.html#comments</comments>
		<pubDate>Fri, 28 Aug 2009 02:36:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[译文]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[代码提示]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/2009/08/%e8%af%91%e6%96%87%e5%9c%a8netbeans-ide%e4%b8%ad%e5%a2%9e%e5%8a%a0codeigniter%e4%bb%a3%e7%a0%81%e6%8f%90%e7%a4%ba.html</guid>
		<description><![CDATA[本文原文来自:
http://www.mybelovedphp.com/2009/01/23/netbeans-revisited-code-completion-for-code-igniter/
根据实际情况删减了少量内容，并增加了配图。在NetBeans 6.7.1简体中文版下面测试通过。转载[......]<p class='read-more'><a href='http://www.yinzhili.com/2009/08/using-code-completion-for-codeigniter-in-netbeans.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>本文原文来自:</p>
<p>http://www.mybelovedphp.com/2009/01/23/netbeans-revisited-code-completion-for-code-igniter/</p>
<p>根据实际情况删减了少量内容，并增加了配图。在NetBeans 6.7.1简体中文版下面测试通过。转载此译文请注明出处。</p>
<p>    对于那些你喜欢的软件，一旦你开始使用它，你就会感觉它就像是为你度身定制的一样。每天你都会一点一点地发现它那隐藏的潜力。这就是开源的好处，还有很多潜在的能力等待我们去发现。大多数商业软件在它们的宣传手册上就已经列出了它们的特性，而最终你开始使用它们的时候你会非常失望。<br />
    NetBeans 6.5是一款好软件，它提供了现成的代码完成和验证功能，支持PHP/HTML/CSS/JavaScript以及jQuery、MooTools等等。<br />
    CodeIgniter是一个PHP快速开发框架，是一个灵活的类MVC系统。NetBeans为CI原生的ActiveRecord类、扩展库和辅助函数提供代码完成功能，帮助你简单地开发CodeIgniter程序。<br />
    你必须先设置好它，下面我们会解释详细步骤：<br />
    如果你将system目录移出了NetBeans项目中包含有application目录的源文件目录(建立多个站点时可能会这么做)，那么这一步就是必不可少的。将CodeIgniter的system目录添加到NetBeans的全局包含目录:<br />
选择<strong>“工具”&gt;“选项”&gt;“PHP”&gt;“添加文件夹”</strong></p>
<div id="attachment_67" class="wp-caption alignnone" style="width: 431px"><img class="size-full wp-image-67" title="译者配图1 添加全局包含目录" src="http://www.yinzhili.com/wp-content/uploads/2009/08/6.JPG" alt="译者配图1 添加全局包含目录" width="421" height="466" /><p class="wp-caption-text">译者配图1 添加全局包含目录</p></div>
<p>这样就可以启用辅助函数的代码完成功能，但是对ActiveRecord或者数据库类的函数不起作用。</p>

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

<p>因此，必须将下面内容添加到控制器中：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
*/</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;">class</span> Stylist <span style="color: #000000; font-weight: bold;">extends</span> Controller</pre></div></div>

<p>现在你可以输入</p>

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

<p>或者</p>

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

<p>你就会看到代码提示了。Wow!<br />
为了进一步简化，请选择“工具”&gt;“选项”&gt;“编辑器”&gt;“代码模板”：<br />
1.<br />
1.“新建” -&gt;“缩写”:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Db</pre></div></div>

<p>2.“扩展文本”:</p>

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

<p>2.<br />
1. “新建” -&gt;“缩写”:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">`codei`</pre></div></div>

<p>2.扩展文本:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
*/</span></pre></div></div>

<p>现在你就可以在NetBeans中输入&#8217;codei&#8217;+Tab或者&#8217;db&#8217;+Tab，就等同于</p>

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

<p>这样很酷，不是吗？</p>
<p>在前段时间的一篇文章里我展示了一种在NetBeans中支持对CodeIgniter框架进行代码完成的方法。… …也许最简单的方式是在CodeIgniter的application及system目录之外的某个路径下创建一个文件，可以命名为 netbeans_ci_code_completion.php，或者别的任何名字都可以，往文件里写入以下内容：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> ?php
<span style="color: #009933; font-style: italic;">/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Table $table
* @property CI_Session $session
* @property CI_FTP $ftp
* ....
 */</span>
<span style="color: #000000; font-weight: bold;">Class</span> Controller <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<p>文件名可以随意，但是扩展名必须是 .php，因为这是给NetBeans而不是CI看的。你可以把它保存到一个名叫temp的目录下，或者也可以保存到.nbproject目录(NetBeans的项目目录)下面，保存到这个目录下的好处是这个目录下的内容不会被同步到服务器上。大多数情况下，我的项目的目录结构是这样的：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">/</span>application
<span style="color: #339933;">/</span>error
<span style="color: #339933;">/</span>images
<span style="color: #339933;">/</span>nbproject
<span style="color: #339933;">/</span>scripts
<span style="color: #339933;">/</span>styles
index<span style="color: #339933;">.</span>php
<span style="color: #339933;">.</span>htaccess</pre></div></div>

<div id="attachment_68" class="wp-caption alignnone" style="width: 758px"><img class="size-full wp-image-68" title="译者配图2 实际效果" src="http://www.yinzhili.com/wp-content/uploads/2009/08/5.JPG" alt="译者配图2 实际效果" width="748" height="318" /><p class="wp-caption-text">译者配图2 实际效果</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2009/08/using-code-completion-for-codeigniter-in-netbeans.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[译文]Francis Healy小传</title>
		<link>http://www.yinzhili.com/2009/08/francis-healy-biography.html</link>
		<comments>http://www.yinzhili.com/2009/08/francis-healy-biography.html#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:30:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[译文]]></category>
		<category><![CDATA[Fran Healy]]></category>
		<category><![CDATA[Travis]]></category>
		<category><![CDATA[传记]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=34</guid>
		<description><![CDATA[Francis (Fran) Healy (1973年7月23日出生于英格兰的斯塔福郡) 是一名苏格兰音乐人。目前他是苏格兰乐队Travis的主唱和主要的歌曲创作者。乐队所发行的五张录音室专辑里的几乎所有歌曲都是由他创作的。

<strong>音乐</strong>

      他被看作是英国最佳的创作人和作曲家之一，他的歌曲创作曾经得到很多艺人的公开赞扬，这些人包括Paul McCartney, Elton John 和 Noel Gallagher (英国Oasis乐队成员)。2005年，酷玩乐队的主唱Chris Martin称自己为“a poor man’s Fran”。在一些访谈中，Fran谈到过那些影响自己的音乐人，比如Joni Mitchell, Paul McCartney 以及 Graham Nash 。Fran还与McCartney 和 Nash一起同台演出过。虽然比较擅长吉它，但是最近他也开始更多地使用钢琴。

<strong>Travis</strong>

      Fran所属的乐队，Travis，是一支来自苏格兰格拉斯哥的另类摇滚乐队。乐队的成员还包括Dougie Payne, Andy Dunlop 以及Neil Primrose。作为英国最受欢迎和尊敬的现代乐队之一，Travis已经两次得到全英音乐奖“年度最佳专辑”的提名，并且常常被认为是诸如Coldplay和Keane这样的英伦乐队的引路人。从1997年的《Good Feeling》开始，他们已经发行了五张录音室专辑，在评论界和商业上都获得了巨大的成功，而他们的现场演出也为人称道。除音乐外，这支可爱的、淳朴而又清新的乐队更是引起了大家的注意。自2003年发行的《12 Memories》之后，他们邀请了Radiohead以及最近担任过Paul McCartney 制作人的Nigel Godrich来为他们新专辑操刀。这几乎是他们出道以来的第十个年头了。

<strong>传记</strong>

      虽然出生于英格兰的斯塔福郡，Fran却是在苏格兰的格拉斯哥长大的，——那是他父母的家乡。她母亲和她那有家庭暴力倾向的丈夫离婚之后，带着他搬回到苏格兰居住。正是由于他父亲的这种恶劣行为，Fran始终不愿和他的父亲有任何联系。Fran说过，儿时对他影响最大的两个人就是他母亲和外祖母。Fran的中学生涯是在格拉斯哥的Holyrood中学度过的。

     上小学时，有一次他穿着裙子演唱了那首苏格兰老歌《Westering Home》，这次演唱获了奖——奖品是一本彭斯的诗集，还有一本“杰出歌唱才能”的证书。但是直到他十几岁之前，他对唱歌都没有多少兴趣。

      他对音乐的狂热爱好始于1986年，那年他13岁，在看过了Roy Orbison 在《The Last Resort With Jonothan Ross》中演唱了那首著名的《Pretty Woman》之后， Fran买了自己的第一把吉它。起初他用吉它演奏的是一些摇滚老歌，比如《Johnny B. Good 》以及Eddie Cochran的《3 Steps To Heaven》。他写的第一首完整的歌是关于他所在学校的校长Peter Mullin的，取名为《Mr. Mullin Blues》，歌词是这样的：
      "...and there was Pete Mullin, with his pie and beans. It was then I smelled it, it filled the room. Then some wee bugger lit a match and the whole place went Ka-BOOM...Where's your tie boy, Pick up that can. Get in line girl, do you understand...Cause his name is big Pete Mullin...And he's a man"

     他在学校的才艺表演中演唱了这首歌，但却没能打动评委。他参加过不少学校的乐队，直到后来，格拉斯哥的乐队Glass Onion的鼓手问他是否想去参加他们的试音。这支乐队后来成为Travis，这个名字来源于Wim Wenders的电影《Paris,Texas》主人公的名字。

      Travis的首支单曲，《All I Want to Do Is Rock》，是Fran在一次前往Greater Cumbrae(克莱德湾的一个小岛)的途中写下的。到那儿去的唯一目的是写一首好歌，Fran也不知道那些灵感到底是怎么来的。尽管从此之后他就以创作人的身份获得了巨大成功，他却从未接受过正式的音乐训练。随着乐队的崭露头角，Fran一直是Travis的主要歌曲创作者，同时也是乐队的发言人以及成员当中最引人注目的一个。
[......]<p class='read-more'><a href='http://www.yinzhili.com/2009/08/francis-healy-biography.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>这是从我过去的博客转过来的一篇文章，英文原文来自维基百科，译文是我很久以前翻译的了，可能现在英文维基百科已经有了新的内容。欢迎转载，但请注明出处，谢谢。</p>
<div class="wp-caption alignnone" style="width: 494px"><img alt="Fran Healy" src="http://www.travisonline.com/archive/pictures/fa8f0b2d58bdb95d2d0f5d13c904b084.jpg" title="Fran Healy" width="484" height="600" /><p class="wp-caption-text">Fran Healy</p></div>
<p>Francis (Fran) Healy (1973年7月23日出生于英格兰的斯塔福郡) 是一名苏格兰音乐人。目前他是苏格兰乐队Travis的主唱和主要的歌曲创作者。乐队所发行的五张录音室专辑里的几乎所有歌曲都是由他创作的。</p>
<p><strong>音乐</strong></p>
<p>      他被看作是英国最佳的创作人和作曲家之一，他的歌曲创作曾经得到很多艺人的公开赞扬，这些人包括Paul McCartney, Elton John 和 Noel Gallagher (英国Oasis乐队成员)。2005年，酷玩乐队的主唱Chris Martin称自己为“a poor man’s Fran”。在一些访谈中，Fran谈到过那些影响自己的音乐人，比如Joni Mitchell, Paul McCartney 以及 Graham Nash 。Fran还与McCartney 和 Nash一起同台演出过。虽然比较擅长吉它，但是最近他也开始更多地使用钢琴。</p>
<p><strong>Travis</strong></p>
<p>      Fran所属的乐队，Travis，是一支来自苏格兰格拉斯哥的另类摇滚乐队。乐队的成员还包括Dougie Payne, Andy Dunlop 以及Neil Primrose。作为英国最受欢迎和尊敬的现代乐队之一，Travis已经两次得到全英音乐奖“年度最佳专辑”的提名，并且常常被认为是诸如Coldplay和Keane这样的英伦乐队的引路人。从1997年的《Good Feeling》开始，他们已经发行了五张录音室专辑，在评论界和商业上都获得了巨大的成功，而他们的现场演出也为人称道。除音乐外，这支可爱的、淳朴而又清新的乐队更是引起了大家的注意。自2003年发行的《12 Memories》之后，他们邀请了Radiohead以及最近担任过Paul McCartney 制作人的Nigel Godrich来为他们新专辑操刀。这几乎是他们出道以来的第十个年头了。</p>
<p><strong>传记</strong></p>
<p>      虽然出生于英格兰的斯塔福郡，Fran却是在苏格兰的格拉斯哥长大的，——那是他父母的家乡。她母亲和她那有家庭暴力倾向的丈夫离婚之后，带着他搬回到苏格兰居住。正是由于他父亲的这种恶劣行为，Fran始终不愿和他的父亲有任何联系。Fran说过，儿时对他影响最大的两个人就是他母亲和外祖母。Fran的中学生涯是在格拉斯哥的Holyrood中学度过的。</p>
<p>     上小学时，有一次他穿着裙子演唱了那首苏格兰老歌《Westering Home》，这次演唱获了奖——奖品是一本彭斯的诗集，还有一本“杰出歌唱才能”的证书。但是直到他十几岁之前，他对唱歌都没有多少兴趣。</p>
<p>      他对音乐的狂热爱好始于1986年，那年他13岁，在看过了Roy Orbison 在《The Last Resort With Jonothan Ross》中演唱了那首著名的《Pretty Woman》之后， Fran买了自己的第一把吉它。起初他用吉它演奏的是一些摇滚老歌，比如《Johnny B. Good 》以及Eddie Cochran的《3 Steps To Heaven》。他写的第一首完整的歌是关于他所在学校的校长Peter Mullin的，取名为《Mr. Mullin Blues》，歌词是这样的：<br />
      &#8220;&#8230;and there was Pete Mullin, with his pie and beans. It was then I smelled it, it filled the room. Then some wee bugger lit a match and the whole place went Ka-BOOM&#8230;Where&#8217;s your tie boy, Pick up that can. Get in line girl, do you understand&#8230;Cause his name is big Pete Mullin&#8230;And he&#8217;s a man&#8221;</p>
<p>     他在学校的才艺表演中演唱了这首歌，但却没能打动评委。他参加过不少学校的乐队，直到后来，格拉斯哥的乐队Glass Onion的鼓手问他是否想去参加他们的试音。这支乐队后来成为Travis，这个名字来源于Wim Wenders的电影《Paris,Texas》主人公的名字。</p>
<p>      Travis的首支单曲，《All I Want to Do Is Rock》，是Fran在一次前往Greater Cumbrae(克莱德湾的一个小岛)的途中写下的。到那儿去的唯一目的是写一首好歌，Fran也不知道那些灵感到底是怎么来的。尽管从此之后他就以创作人的身份获得了巨大成功，他却从未接受过正式的音乐训练。随着乐队的崭露头角，Fran一直是Travis的主要歌曲创作者，同时也是乐队的发言人以及成员当中最引人注目的一个。</p>
<p>      从2000年开始，德国摄影家、前化妆师Nora Kryst成为他的女友和未婚妻。2006年3月，他们的第一个孩子，这个名叫Clay的小男孩诞生了。儿子随母亲姓Kryst。他们住在伦敦，但是他们在纽约的SoHo区也购置了一套公寓。最近在慕尼黑演出时，他还透露过自己将来打算移民到德国去。</p>
<p><strong>行为主义</strong></p>
<p>      Fran参与了&#8221;让贫穷成为历史&#8221;这一运动，还参演了在伦敦和爱丁堡举行的Live 8音乐会。他还参与了慈善歌曲《Do They Know It&#8217;s Christmas?》的录制，Fran和他的朋友Nigel Godrich还是这次活动的组织者。</p>
<p>      到目前为止他曾经两次随“拯救儿童组织”前往苏丹，这是全球最大规模的一次目的在于为上千万儿童提供救助的运动。</p>
<p>      Fran也参加了多次反对伊拉克战争的游行运动，并担任过那些运动的发言人。</p>
<p><strong>杂项</strong></p>
<p>      Fran的音乐受Joni Mitchell, Paul McCartney 和 Graham Nash的影响。他非常喜欢Arcade Fire乐队。同时他也非常喜欢单口相声(stand-up comedy)。</p>
<p>      小时候Fran就已经赢得了空手道黑带。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2009/08/francis-healy-biography.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
