<?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; PHP</title>
	<atom:link href="http://www.yinzhili.com/category/php/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>为NetBeans添加Git支持</title>
		<link>http://www.yinzhili.com/2010/03/add-git-support-for-netbeans.html</link>
		<comments>http://www.yinzhili.com/2010/03/add-git-support-for-netbeans.html#comments</comments>
		<pubDate>Mon, 29 Mar 2010 07:58:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=189</guid>
		<description><![CDATA[说明：以下操作在NetBeans 6.8下进行，不同版本可能会有细微的差别。
<a href="http://www.yinzhili.com/wp-content/uploads/2010/03/0.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/0.jpg" alt="Git logo" title="Git logo" width="519" height="98" class="aligncenter size-full wp-image-191" /></a>
<strong>什么是Git?</strong>
Git 是 Linux 的创始人 Linus Torvalds 为了帮助管理 Linux® 内核开发而开发的一个开放源码的版本控制软件。我们可以自己下载这个软件用来管理自己的软件开发项目。与常用的版本控制[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/03/add-git-support-for-netbeans.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>说明：以下操作在NetBeans 6.8下进行，不同版本可能会有细微的差别。</p>
<p><a href="http://www.yinzhili.com/wp-content/uploads/2010/03/0.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/0.jpg" alt="Git logo" title="Git logo" width="519" height="98" class="aligncenter size-full wp-image-191" /></a><br />
<strong>什么是Git?</strong><br />
Git 是 Linux 的创始人 Linus Torvalds 为了帮助管理 Linux® 内核开发而开发的一个开放源码的版本控制软件。我们可以自己下载这个软件用来管理自己的软件开发项目。与常用的版本控制工具 CVS, Subversion 等不同，它采用了分布式版本库的方式，不必服务器端软件支持，使源代码的发布和交流极其方便。 Git 的速度很快，这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的合并跟踪（merge tracing）能力。</p>
<p><strong>如何安装Git?</strong><br />
Git最初是为了Linux而生，所以一开始并不能直接在Windows下使用。<br />
但是好在Git的开发者们并没有忘记Windows下的程序员，有个名为msysgit开源项目提供了支持：</p>
<p>http://code.google.com/p/msysgit/</p>
<p>访问以下地址获取msysgit的最新版本：</p>
<p>http://code.google.com/p/msysgit/downloads/list</p>
<p>安装过程很简单，就不做过多叙述了。安装完成后我们会发现系统右键菜单会增加几个与Git有关的项目。<br />
Git主要是命令行工具，通过命令行来进行管理，当然也有Windows下的GUI工具如TortoiseGit，但这些都不是本文要叙述的重点。</p>
<p><strong>集成到NetBeans当中</strong><br />
下面叙述一下如何在NetBeans中集成Git支持。<br />
众所周知NetBeans是一个非常优秀而且是开源的IDE，支持Java/PHP/Ruby等多种主流语言的开发，我主要用来开发PHP项目。默认情况下NetBeans已经内置了Kenai、CVS、Subversion、Mecurial等团队开发工具的支持，现在我们要为他添加Git的支持。<br />
开源项目NbGit是一款NetBeans插件，提供了Git支持，因此首先必须得到NbGit的最新版本：</p>
<p>http://code.google.com/p/nbgit/downloads/list</p>
<p>选择下载扩展名为.nbm的插件即可。然后打开NetBeans，依次选择“工具”->“插件”->“已下载”->“添加插件”，然后选中我们刚刚下载到的nbgit-0.3.nbm文件，然后安装即可。<br />
<a href="http://www.yinzhili.com/wp-content/uploads/2010/03/1.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/1.jpg" alt="图1 安装NbGit" title="图1 安装NbGit" width="606" height="556" class="aligncenter size-full wp-image-192" /></a><br />
需要说明的是安装过程中会有一个未签名的警告，不用管它，单击“继续”来安装它。稍等片刻即可安装完成。<br />
<a href="http://www.yinzhili.com/wp-content/uploads/2010/03/2.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/2.jpg" alt="图2 警告" title="图2 警告" width="500" height="453" class="aligncenter size-full wp-image-193" /></a><br />
此时再打开NetBeans的“团队开发”菜单，我们会发现多了Git这个选项，这就说明已经安装成功。<br />
<a href="http://www.yinzhili.com/wp-content/uploads/2010/03/3.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/3.jpg" alt="图3 菜单" title="图3 菜单" width="405" height="403" class="aligncenter size-full wp-image-194" /></a></p>
<p><strong>如何使用</strong><br />
我们打开“团队开发”->“Git”->“Clone Other”即可克隆一份repository。同样，项目的右键菜单中也有相应的命令可供使用。<br />
<a href="http://www.yinzhili.com/wp-content/uploads/2010/03/5.jpg"><img src="http://www.yinzhili.com/wp-content/uploads/2010/03/5.jpg" alt="图4 Git菜单命令" title="图4 Git菜单命令" width="427" height="758" class="aligncenter size-full wp-image-196" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/03/add-git-support-for-netbeans.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Windows下编译适用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下载)</title>
		<link>http://www.yinzhili.com/2010/02/build-eaccelerator-for-php-5-2-12-or-5-2-13-on-windows.html</link>
		<comments>http://www.yinzhili.com/2010/02/build-eaccelerator-for-php-5-2-12-or-5-2-13-on-windows.html#comments</comments>
		<pubDate>Fri, 26 Feb 2010 08:10:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[eaccelerator]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=145</guid>
		<description><![CDATA[最近PHP 5.2.12及5.2.13发布了，修复了多个bug，因此升级到这一版本是非常必要的。
首先说明一下我的一些参数：
操作系统: Windows 7 Ultimate
Web Server：Apache 2.2.14
Visual Studio: 6.0 (别嫌弃它版本老，即便是在Win7下[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/02/build-eaccelerator-for-php-5-2-12-or-5-2-13-on-windows.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>最近PHP 5.2.12及5.2.13发布了，修复了多个bug，因此升级到这一版本是非常必要的。</p>
<p>首先说明一下我的一些参数：<br />
操作系统: Windows 7 Ultimate<br />
Web Server：Apache 2.2.14<br />
Visual Studio: 6.0 (别嫌弃它版本老，即便是在Win7下它还是能用的，当然你用Visual Studio 2008也行)</p>
<p>什么是eAccelerator？<br />
eaccelerator是一个自由开放源码php加速器，优化和动态内容缓存，提高了性能php脚本的缓存性能，使得PHP脚本在编译的状态下，对服务器的开销几乎完全消除。 它还有对脚本起优化作用，以加快其执行效率。</p>
<p>1.首先下载编译好的PHP二进制包。(稍后会用到) 地址如下：<br />
<span style="color: #3366ff;">http://cn.php.net/distributions/php-5.2.12-Win32.zip</p>
<p>http://cn.php.net/distributions/php-5.2.13-Win32.zip</span></p>
<p>2.再下载PHP的源代码，稍后编译eAccelerator的时候会用到。地址如下：<br />
<span style="color: #3366ff;">http://cn2.php.net/get/php-5.2.12.tar.bz2/from/a/mirror</p>
<p>http://cn2.php.net/get/php-5.2.13.tar.bz2/from/a/mirror</span></p>
<p>3.升级PHP：<br />
这一过程很简单，首先关闭Apache，然后将压缩包里解压出来的所有文件替换掉原来的文件即可。例如你原来的PHP程序文件位于 D:\php，那么请将php-5.2.13-Win32.zip(或者php-5.2.12-Win32.zip)解压到这一路径下覆盖原有文件。</p>
<p>4.启动Apache，我们会收到一条报错信息，Apache无法启动。打开Apache的日志文件(此文件路径为apache\logs\error.log)，我们会看到这样的提示信息：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">PHP Warning<span style="color: #339933;">:</span>  <span style="color: #009900;">&#91;</span>eAccelerator<span style="color: #009900;">&#93;</span> This build of <span style="color: #0000ff;">&quot;eAccelerator&quot;</span> was compiled <span style="color: #b1b100;">for</span> PHP version 5<span style="color: #339933;">.</span>2<span style="color: #339933;">.</span>11<span style="color: #339933;">.</span>
Rebuild it <span style="color: #b1b100;">for</span> your PHP version <span style="color: #009900;">&#40;</span>5<span style="color: #339933;">.</span>2<span style="color: #339933;">.</span>13<span style="color: #009900;">&#41;</span> or download precompiled binaries<span style="color: #339933;">.</span></pre></div></div>

<p>意思就是当前的eAccelerator是为PHP 5.2.11编译的，不适用于5.2.13。我们应该重新编译一份eAccelerator。</p>
<p>5.下载一份eAccelerator的源码。地址如下：<br />
<span style="color: #3366ff;">http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip</span></p>
<p>6.将第2步骤下载到的PHP源码解压到某一目录下，例如 E:\php。然后打开你的PHP程序目录(也就是你的php.exe所在的目录)，找到其中dev子目录下的php5ts.lib文件，把它复制到E:\php这一位置。</p>
<p>7.打开E:\php\ext，在这下面创建一个目录命名为eaccelerator，然后将第5步骤下载到的压缩包解压到此目录下。此时的目录结构应该是这样的：</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 198px"><a href="http://www.yinzhili.com/wp-content/uploads/2009/12/1.png"><img class="size-medium wp-image-146" title="图1" src="http://www.yinzhili.com/wp-content/uploads/2009/12/1.png" alt="图1" width="188" height="502" /></a><p class="wp-caption-text">图1</p></div>
<p>8.打开win32子目录，其中应该有一个名叫eAccelerator.dsw的项目文件，用VS开发环境打开它(比如我用的就是VS6.0)，打开&#8221;组件&#8221;&gt;&#8221;配置&#8221;，选中 Win32 Release PHP5 这一选项，单击“关闭”。</p>
<div id="attachment_147" class="wp-caption aligncenter" style="width: 456px"><a href="http://www.yinzhili.com/wp-content/uploads/2009/12/2.png"><img class="size-medium wp-image-147" title="图2" src="http://www.yinzhili.com/wp-content/uploads/2009/12/2.png" alt="图2" width="446" height="228" /></a><p class="wp-caption-text">图2</p></div>
<p>9.按下F7键(或选择工具条中的编辑按钮)开始编译，稍等片刻，编译成功，你就可以在win32目录下看到生成了一个Release子目录，打开这个子目录，你就会看到已经生成了eAccelerator.dll文件。<br />
将这个文件复制到你的PHP扩展目录下(如D:\php\ext)，替换掉原有的文件，然后启动Apache，你就会发现eAccelerator已经成功加载了。</p>
<div id="attachment_148" class="wp-caption aligncenter" style="width: 615px"><a href="http://www.yinzhili.com/wp-content/uploads/2009/12/3.png"><img class="size-full wp-image-148" title="图3" src="http://www.yinzhili.com/wp-content/uploads/2009/12/3.png" alt="图3" width="605" height="258" /></a><p class="wp-caption-text">图3</p></div>
<h3>或者</h3>
<p><a href="http://www.yinzhili.com/downloads/binary/eAccelerator_dll_for_php_5_2_13.zip">猛击此处下载编译好的dll文件</a></p>
<p>Visual Studio 2008下编译可能会出现类似于“<span style="color: #ff0000;">不能分配常量大小为 0 的数组</span>”这样的报错信息，解决办法如下：</p>
<blockquote><p>找到源码目录的main子目录，打开它，找到以下文件：<br />
config.w32.h<br />
 <br />
在这个文件里查找 #define _USE_32BIT_TIME_T 1，将这一行注释掉。再重新编译即可。</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/02/build-eaccelerator-for-php-5-2-12-or-5-2-13-on-windows.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>通过伪造Referer来解决外链问题</title>
		<link>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html</link>
		<comments>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html#comments</comments>
		<pubDate>Fri, 08 Jan 2010 07:47:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[外链]]></category>
		<category><![CDATA[盗链]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=163</guid>
		<description><![CDATA[什么是Referer?
Referer是HTTP Header的一个字段，当浏览器向服务器发送请求的时候一般会包含Referer信息，这一字段保存的是访客的来源URI。
以PHP为例，通过输出$_SERVER变量，你就可以看到HTTP_REFERER信息：
<div id="attachment_164" class="wp-caption aligncenter" style="width: 509px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png" alt="图1" title="图1" width="499" height="197" class="size-full wp-image-164" /></a>图1</div>
如今很多知名的网络相册(如网易相册、[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>什么是Referer?<br />
Referer是HTTP Header的一个字段，当浏览器向服务器发送请求的时候一般会包含Referer信息，这一字段保存的是访客的来源URI。</p>
<p>以PHP为例，通过输出$_SERVER变量，你就可以看到HTTP_REFERER信息：<br />
<div id="attachment_164" class="wp-caption aligncenter" style="width: 509px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png" alt="图1" title="图1" width="499" height="197" class="size-full wp-image-164" /></a><p class="wp-caption-text">图1</p></div></p>
<p>如今很多知名的网络相册(如网易相册、百度相册、51相册等等)都限制了外链，我不知道它们用什么方式实现，但是目前而言大多数防盗链的机制都是基于对Referer的判断来实现的。<br />
比如说，我有一个网站，其中一个页面地址是 http://example.com/test.html，里面需要外链一张猫扑的图片，因此，image.html里面可以这么写：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://upload3.mop.com/upload3/2009/12/18/08/1261139622733.jpg&quot;/&gt;</pre></td></tr></table></div>

<p>但是我们会发现这样外链的图片是无法显示出来的。原因在于，当我们访问http://example.com/test.html时，浏览器会向upload3.mop.com也就是猫扑的服务器发送一个请求，这一请求中包含的Referer信息应该就是这样：</p>
<p>http://example.com/test.html</p>
<p>这样，猫扑的服务器就可以判断出这是一个来自外部网站的请求，从而予以拒绝，这样我们就无法显示这张图片了。</p>
<p>那么，应该如何解决这一问题呢? 用火狐浏览器的可能知道，火狐有个插件叫做RefControl，可以用它来伪造Referer，从而正常显示图片。但是如果你是站长，你总不能要求你的访客都使用火狐浏览器对吧？即便你所有的访客都使用火狐浏览器，你也很难保证他们都安装了RefControl这个插件。<br />
这样，我们只能从程序方面入手解决问题：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #339933;">=</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #000088;">$path</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$path</span><span style="color: #339933;">=</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;GET <span style="color: #006699; font-weight: bold;">$path</span> HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Host: <span style="color: #006699; font-weight: bold;">$host</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Accept: */*<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Referer: http://<span style="color: #006699; font-weight: bold;">$host</span>/<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000088;">$Content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pos</span><span style="color: #339933;">=</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$head</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$text</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #000088;">$pos</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$head</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>实际上这就是通过伪造Referer来实现我们想要的效果。<br />
我们把这段程序保存为redirect.php，放到服务器上，例如http://example.com/redirect.php，那么，接下来我们只需要将原来的外链图片地址经由这段程序处理，就可以正常显示。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://example.com/redirect.php?url=http://upload3.mop.com/upload3/2009/12/18/08/1261139622733.jpg&quot;/&gt;</pre></td></tr></table></div>

<p>效果如下：<br />
<div id="attachment_165" class="wp-caption aligncenter" style="width: 368px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/02.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/02.png" alt="图2" title="图2" width="358" height="427" class="size-full wp-image-165" /></a><p class="wp-caption-text">图2</p></div></p>
<p>由此可见，Referer信息不一定可靠，因为它是可以伪造出来的，通过这一手段来防盗链并不是一个完美的办法。<br />
同时，我们也应该尽可能少的外链文件，毕竟大多数服务器都有带宽的限制和成本的考虑，限制外链对它们的生存也是很重要的。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wordpress常用时间参数</title>
		<link>http://www.yinzhili.com/2010/01/wordpress-time-parameters.html</link>
		<comments>http://www.yinzhili.com/2010/01/wordpress-time-parameters.html#comments</comments>
		<pubDate>Sat, 02 Jan 2010 05:01:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=161</guid>
		<description><![CDATA[使用Wordpress国外主题的时候常常要改动其中的时间参数，因为老外他们使用的时间格式和我们大不一样，比如说同样是一个日期：2010年1月2日，美国英语里面可能写成 Jan 2nd 2010，英国英语里则是 2nd Jan 2010，而我们汉语里是 2010年1月2日。好在Wordpress提供了[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/01/wordpress-time-parameters.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>使用Wordpress国外主题的时候常常要改动其中的时间参数，因为老外他们使用的时间格式和我们大不一样，比如说同样是一个日期：2010年1月2日，美国英语里面可能写成 Jan 2nd 2010，英国英语里则是 2nd Jan 2010，而我们汉语里是 2010年1月2日。好在Wordpress提供了很多时间参数，用户可以输出自定义的日期，这一点和PHP里面的date函数有点类似。</p>
<p>下面是一些常用的参数：</p>
<p>* a代表小写的英语的上下午，如am、pm<br />
* A代表大写的英语的上下午，如AM、PM<br />
* d代表英语的日期（小于10仍为两位数写法），如05、12<br />
* D代表中文的星期，如五、七<br />
* F代表中文的月份（包括“月”这个字），如五月、十二月<br />
* g代表英语的小时（小于10为一位数写法），如5、12<br />
* G代表英语的小时（小于10仍为两位数写法），如05、12<br />
* h代表英语的分钟（小于10为一位数写法），如5、12<br />
* H代表英语的分钟（小于10仍为两位数写法），如05、12<br />
* j代表英语的日期（小于10为一位数写法），如08-02-09<br />
* l代表中文的星期（包括“星期”这两个字），如星期五、星期七<br />
* m代表英语的月份（小于10仍为两位数写法），如05、12<br />
* M代表英语的月份（以单词的形式显示），如Jun<br />
* n代表英语的月份（小于10为一位数写法），如5、12<br />
* O代表英语的时区，如+0800<br />
* r代表完整的日期时间，如Tue, 06 Jun 2006 18:37:11 +0800<br />
* S代表日期的序数后缀，如st、th<br />
* T代表英语的时区（以单词的形式显示），如CST<br />
* w代表英语的星期，如5、7<br />
* W代表周数，如23<br />
* y代表两位数年份，如07、08<br />
* Y代表四位数年份，如2007、2008<br />
* z代表天数，如156</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/01/wordpress-time-parameters.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>phpMyAdmin升级到3.2.x版遇到的常见问题</title>
		<link>http://www.yinzhili.com/2009/09/problems-you-will-encounter-when-using-phpmyadmin3.html</link>
		<comments>http://www.yinzhili.com/2009/09/problems-you-will-encounter-when-using-phpmyadmin3.html#comments</comments>
		<pubDate>Wed, 30 Sep 2009 02:32:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[phpMyAdmin]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=93</guid>
		<description><![CDATA[phpMyAdmin是一个用PHP编写的基于Web的MySQL管理工具。从事过PHP+MySQL开发的朋友肯定接触过它，非常方便。最近把电脑上的phpMyAdmin升级到了3.2.2版，遇到了一些问题，在此总结一下。
1. “空密码登录被禁止” &#160;问题
很多时候我们在本机测试时会将root[......]<p class='read-more'><a href='http://www.yinzhili.com/2009/09/problems-you-will-encounter-when-using-phpmyadmin3.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>phpMyAdmin是一个用PHP编写的基于Web的MySQL管理工具。从事过PHP+MySQL开发的朋友肯定接触过它，非常方便。最近把电脑上的phpMyAdmin升级到了3.2.2版，遇到了一些问题，在此总结一下。</p>
<p>1. <font color="Red">“空密码登录被禁止”</font> &nbsp;问题<br />
很多时候我们在本机测试时会将root用户密码设置为空。但升级到phpMyAdmin 3.2.2版的时候，会遇到无法以空密码登录root用户的情况。怎么解决呢? 请参照如下步骤：<br />
(1) 找到你的phpMyAdmin程序所在的目录，这个根据你个人的情况有所不同。<br />
(2) 打开 phpMyAdmin\libraries\ 路径下的 <strong>config.default.php</strong> 这个文件，找到下面这行</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Servers'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AllowNoPassword'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>将其修改为</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Servers'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AllowNoPassword'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>(3) 重新访问你的phpMyAdmin，你会发现空密码的root用户也可以登录了。</p>
<p>2. <font color="Red">“配置文件现在需要一个短语密码”</font>&nbsp;问题<br />
在登录进去之后可能会看到<font color="Red">“配置文件现在需要一个短语密码”</font>这条警告信息。这是因为你没有设置一个用来给Cookie加密的密钥。解决方法还是首先打开上文所说的 config.default.php 文件，并找到下面这行</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blowfish_secret'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span></pre></div></div>

<p>将其修改为</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blowfish_secret'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'你的密钥'</span><span style="color: #339933;">;</span></pre></div></div>

<p>说明: 这里的“你的密钥”是你所设置的密钥字符串，可以随意设置。<br />
修改保存之后，重新登录phpMyAdmin就可以了。</p>
<p>3. <font color="Red">“无法载入mcrypt扩展”</font>&nbsp;问题<br />
这个问题与phpMyAdmin没有多少关系，是因为你的 PHP 运行环境没有开启mcrypt扩展。以Windows下为例，首先你要确保PHP目录的ext子目录下有 php_mcrypt.dll 这个文件，没有的话去网上下载一个。然后打开你的 php.ini 文件，找到下面这行</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">;</span>extension<span style="color: #339933;">=</span>php_mcrypt<span style="color: #339933;">.</span>dll</pre></div></div>

<p>去掉这行行首的分号，保存，然后重启Apache就可以开启mcrypt支持了。这个时候重新登陆phpMyAdmin，此问题就已经解决。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2009/09/problems-you-will-encounter-when-using-phpmyadmin3.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用TCPDF输出完美的中文PDF文档</title>
		<link>http://www.yinzhili.com/2009/08/using-tcpdf-to-generate-pdf-in-chinese.html</link>
		<comments>http://www.yinzhili.com/2009/08/using-tcpdf-to-generate-pdf-in-chinese.html#comments</comments>
		<pubDate>Thu, 20 Aug 2009 10:03:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[DroidSansFallback]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[tcpdf]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=47</guid>
		<description><![CDATA[TCPDF是一个用于快速生成PDF文件的PHP5函数包。TCPDF基于FPDF进行扩展和改进。支持UTF-8，Unicode，HTML和XHTML。在基于PHP开发的Web应用中，使用它来输出PDF文件是绝佳的选择。但毕竟这款开源软件是外国人开发的，对中文的支持总不是那么尽如人意，因此我们需要对它作[......]<p class='read-more'><a href='http://www.yinzhili.com/2009/08/using-tcpdf-to-generate-pdf-in-chinese.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>TCPDF是一个用于快速生成PDF文件的PHP5函数包。TCPDF基于FPDF进行扩展和改进。支持UTF-8，Unicode，HTML和XHTML。在基于PHP开发的Web应用中，使用它来输出PDF文件是绝佳的选择。但毕竟这款开源软件是外国人开发的，对中文的支持总不是那么尽如人意，因此我们需要对它作进一步的增强。<br />
首先要下载TCPDF。访问http://www.tcpdf.org ，单击导航条上的&#8221;Download&#8221;链接，即可下载到最新版本的TCPDF压缩包。因为里面包含了范例和字体，因此文件大概有10M左右。下载完后解压它，会得到一个名为tcpdf的目录，此目录的结构大概是这样的：</p>
<div id="attachment_48" class="wp-caption alignnone" style="width: 134px"><img class="size-full wp-image-48" title="图1 TCPDF目录结构图" src="http://www.yinzhili.com/wp-content/uploads/2009/08/1.gif" alt="TCPDF目录结构图" width="124" height="267" /><p class="wp-caption-text">TCPDF目录结构图</p></div>
<p>将此目录移动到Apache的网页根目录下(假设您使用的是Apache，并且使用80端口，如有不同请根据实际情况修改)，打开浏览器，访问 http://localhost/tcpdf/ 应该就可以看到此目录下的文件和文件夹列表。由于TCPDF已经自带了一些范例，因此为了节省时间，我们可以直接对范例进行修改再使用。打开tcpdf目录下的examples目录，下面有50多个范例文件，其中example_038.php就是用来测试东亚字体的(比如简体/繁体中文、日文等等)，打开此文件，找到$pdf-&gt;SetFont 这一行，修改为：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stsongstdlight'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这一行代码的作用是设置PDF正文所用的字体及字号。其中&#8221;stsongstdlight&#8221;表示&#8221;STSongStdLight&#8221;字体，这是Adobe Reader的默认简体中文字体，TCPDF中已经内置这个字体的配置文件，我们只需直接调用即可。接下来，将$pdf->Cell那一段修改为:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cell</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'敏捷的棕毛狐狸跃过那只懒狗。'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'C'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cell</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'The quick brown fox jumps over the lazy dog.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'C'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cell</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1234567890'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'C'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>保存，然后访问 http://localhost/tcpdf/examples/example_038.php 就可以生成一份PDF文档了:</p>
<div id="attachment_49" class="wp-caption alignnone" style="width: 431px"><img class="size-full wp-image-49" title="图2 使用默认中文字体生成的PDF文件" src="http://www.yinzhili.com/wp-content/uploads/2009/08/2.gif" alt="使用默认中文字体生成的PDF文件" width="421" height="112" /><p class="wp-caption-text">使用默认中文字体生成的PDF文件</p></div>
<p>这种方式生成的PDF文件的优点是：文件体积小，生成快速。但也有缺点是，没有嵌入中文字体，只限于安装了Adobe Reader之后才能正常显示。那万一用户使用的是FoxIt Reader或者是Linux操作系统呢？显示效果就不一样了。因此，为了保证生成的PDF文件在任何环境下都有同样的显示效果，嵌入字体是必需的。<br />
Windows下有很多中文字体，但是我们要用在TCPDF中的中文字体有下面几个要求：</p>
<ul>
<li>支持Unicode，因为TCPDF支持的是Unicode;</li>
<li>体积越小越好;</li>
<li>最好是也支持繁体中文;</li>
</ul>
<p>这样看来，微软雅黑以及方正的一些字体都符合要求。但是他们都是商业字体，而且个头都不小，以微软雅黑为例，msyh.ttf 文件就超过10M，如果使用它，生成的PDF文件体积也会很大。<br />
综合考虑，我觉得&#8221;Droid Sans Fallback&#8221;字体符合要求：</p>
<ul>
<li>首先它是免费字体;</li>
<li>其次它也是Unicode编码，支持简体繁体中文以及日文韩文等等;</li>
<li>然后它的体积很小，不超过5M。 </li>
</ul>
<p>然而TCPDF不支持TTF字体文件，因此我们先将它转换成TCPDF支持的格式，然后再使用。在TCPDF目录下有个fonts子目录，这个子目录下又有个utils，这里面带有一个字体转换工具ttf2ufm.exe。下面是转换的步骤：<br />
在网上很容易找到Droid Sans Fallback字体的下载链接，因此在这里就没有必要多做描述了。我们把下载到的 DroidSansFallback.ttf 复制到 TCPDF\fonts\utils 下面，然后打开Windows的命令行，切换到此路径下，输入如下命令：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">D<span style="color: #339933;">:</span>\www\tcpdf\fonts\utils<span style="color: #339933;">&gt;</span>ttf2ufm <span style="color: #339933;">-</span>a <span style="color: #339933;">-</span>F DroidSansFallback<span style="color: #339933;">.</span>ttf</pre></div></div>

<p>等待一段时间，等到命令行窗口显示</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;Finished - font files created&quot;</span></pre></div></div>

<p>之后，可以发现此目录下生成了DroidSansFallback.afm,DroidSansFallback.t1a 和 DroidSansFallback.ufm 这三个文件。<br />
在命令行中输入</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;C:\Program Files\WAMP\PHP5\php.exe&quot;</span> <span style="color: #339933;">-</span>q makefont<span style="color: #339933;">.</span>php DroidSansFallback<span style="color: #339933;">.</span>ttf DroidSansFallback<span style="color: #339933;">.</span>ufm</pre></div></div>

<p>说明：这里的&#8221;C:\Program Files\WAMP\PHP5\php.exe&#8221;为php.exe文件所在的路径，请根据实际情况输入。回车运行之后，稍等片刻，命令行窗口中会提示</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Font definition <span style="color: #990000;">file</span> generated</pre></div></div>

<p>至此大功告成。将生成的droidsansfallback.php、droidsansfallback.z以及droidsansfallback.ctg.z这三个文件复制到 TCPDF\fonts 下面即可。<br />
打开example_038.php文件，将</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stsongstdlight'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>修改为</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'droidsansfallback'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这样就能够调用我们刚才生成的字体，再访问 http://localhost/tcpdf/examples/example_038.php 就可以看到重新生成的PDF文档。</p>
<div id="attachment_50" class="wp-caption alignnone" style="width: 489px"><img class="size-full wp-image-50" title="图3 嵌入DroidSansFallback字体后的PDF文档" src="http://www.yinzhili.com/wp-content/uploads/2009/08/3.gif" alt="嵌入DroidSansFallback字体后的PDF文档" width="479" height="105" /><p class="wp-caption-text">嵌入DroidSansFallback字体后的PDF文档</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2009/08/using-tcpdf-to-generate-pdf-in-chinese.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
