毫无疑问,PHP5的推出对网络应用程序的发展产生了深远意义。这还不仅仅是因为它对于PHP4高度的向后兼容性。
当然,PHP5数据包具备强大的对象模型和一整套新型函数与库,更不用说它其中便携式异常机制,这一机制赋予了它有效处理错误和异常事件的能力。 通过PHP在服务器上写入文件
当然,当我们使用“函数”这一词时,我们所谈论的并不仅是通常所指的执行指定任务(如从文件中读取数据或为所供变量应用过滤器)的那一概念。我们所指的还包括一些神奇功能(比如PHP 5 的克隆函数),即那些不具备默认的确定执行任务且可以被PHP引擎自动调用以响应某些特定事件的函数。
开发对象指向型PHP应用程序时,_set(),_get()和_call()方法就种神奇函数的典型代表,但是还有其他一些神奇函数值得开发员深入分析。在本文里,我们将集中讨论“_clone()”方法,顾名思义,使用“clone”PHP关键词时可以从幕后调用这一方法。
复制对象的时候从幕后触发函数
就像我们在之前提到的,使用clone关键词的时候,_clone()函数(克隆函数)会被自动援引。为了简洁起见,现在我们只谈论用来创建指定对象单独副本的clone关键词,而不谈及创建引用。 用PHP把HTML添加到多个文件中
现在,返回_clone函数,可以给它一个明确的任务指示。为了实现这一点,我们看看下面的代码:
<ol class="dp-c"> <li class="alt"><span><span> </span><span class="keyword">class</span><span> User </span></span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// constructor (not implemented) </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> _construct(){} </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// set undeclared property in a restrictive way </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __set(</span><span class="vars">$property</span><span>, </span><span class="vars">$value</span><span>) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">if</span><span> (in_array(</span><span class="vars">$property</span><span>, </span><span class="keyword">array</span><span>(</span><span class="string">'fname'</span><span>, </span><span class="string">'lname'</span><span>, </span><span class="string">'email'</span><span>)) === TRUE) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="vars">$this</span><span>-></span><span class="vars">$property</span><span> = </span><span class="vars">$value</span><span>; </span> </li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// get undeclared property </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __get(</span><span class="vars">$property</span><span>) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">if</span><span> (isset(</span><span class="vars">$this</span><span>-></span><span class="vars">$property</span><span>)) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>-></span><span class="vars">$property</span><span>; </span> </li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// single point to fetch user data </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __call(</span><span class="vars">$method</span><span>, </span><span class="vars">$args</span><span>) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">if</span><span> (</span><span class="vars">$method</span><span> === </span><span class="string">'fetch'</span><span> AND </span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$args</span><span>) === FALSE) </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>-></span><span class="vars">$args</span><span>[0]; </span> </li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// implement __clone( method </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __clone() </span> </li> <li class="alt"><span> </span></li> <li><span>{ </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="func">echo</span><span> </span><span class="string">'Cloning user object.'</span><span>; </span> </li> <li class="alt"><span> </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>} </span></li> </ol>
在这个特定示例中,我们要给上述的_clone方法一个不太显眼的任务,以便帮助你更好地理解,但是要相信该函数可以用来执行更为复杂的任务。不管本文中所演示的_clone函数是如何简单,复制简单的User类实例时,该函数非常有助于向我们展示PHP引擎是怎样调用函数的。
复制对象的时候调用Clone方法
要明白PHP解释程序是如何调用_clone()函数最好的方法是观察一个演示如何复制简单User类实例的具体示例。
我们制作了一个小型脚本,该脚本使用克隆函数中的clone关键字来复制一个用户对象从而触发有关_clone()方法的调用。该脚本的定义如下所示:
<ol class="dp-c"> <li class="alt"><span><span> </span><span class="vars">$user</span><span> = </span><span class="keyword">new</span><span> User(); </span></span></li> <li> <span></span><span class="vars">$user</span><span>->fname = </span><span class="string">'Alejandro'</span><span>; </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="vars">$user</span><span>->lname = </span><span class="string">'Gervasio'</span><span>; </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="vars">$user</span><span>->email = </span><span class="string">'alejandro@mydomain.com'</span><span>; </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// display user data </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="func">echo</span><span> </span><span class="string">'First Name : '</span><span> . </span><span class="vars">$user</span><span>->fetch(</span><span class="string">'fname'</span><span>) . </span><span class="string">' Last Name : '</span><span> . </span><span class="vars">$user</span><span>->fetch(</span><span class="string">'lname'</span><span>) .<br></span><span class="string">' Email : '</span><span> . </span><span class="vars">$user</span><span>->fetch(</span><span class="string">'email'</span><span>); </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">/* </span> </li> <li class="alt"><span> </span></li> <li><span><span class="comment">displays the following </span> </span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com </span> </span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">*/</span><span> </span></span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">// clone user object </span><span> </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="vars">$newuser</span><span> = clone </span><span class="vars">$user</span><span>; </span> </li> <li class="alt"><span> </span></li> <li> <span></span><span class="comment">/* </span> </li> <li class="alt"><span> </span></li> <li><span><span class="comment">displays the following </span> </span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">Cloning user object. </span> </span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">*/</span><span> </span></span></li> </ol>
这段代码易于编写,也易于理解。正如你所看到的,一旦脚本在User类中生成一个对象并创建一些未申明的属性,它就会复制这个对象。这个进程会自动调用相关的_clone()方法。
这一具体方法支持更复杂更有益的任务执行。因此,如果PHP5中附带的这些神奇函数已经引起你的注意,你或许想通过使用这些函数改善自己的编程技能了。这样的尝试将极具启发性。
结语
本文中,我们讨论了如何实施和使用_clone()函数。对象通过clone关键词复制时,该函数会被自动引用;这就是PHP 5克隆函数的强大之处。