<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Magic Methods</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类与对象(PHP 5)" HREF="language.oop5.html"><LINK REL="PREVIOUS" TITLE="Patterns" HREF="language.oop5.patterns.html"><LINK REL="NEXT" TITLE="Final Keyword" HREF="language.oop5.final.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.oop5.patterns.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 19. 类与对象(PHP 5)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop5.final.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.magic" >Magic Methods</A ></H1 ><P > The function names <TT CLASS="literal" >__construct</TT >, <TT CLASS="literal" >__destruct</TT > (see <A HREF="language.oop5.decon.html" >Constructors and Destructors</A >), <TT CLASS="literal" >__call</TT >, <TT CLASS="literal" >__get</TT >, <TT CLASS="literal" >__set</TT >, <TT CLASS="literal" >__isset</TT >, <TT CLASS="literal" >__unset</TT > (see <A HREF="language.oop5.overloading.html" >Overloading</A >), <TT CLASS="literal" >__sleep</TT >, <TT CLASS="literal" >__wakeup</TT >, <TT CLASS="literal" >__toString</TT >, <TT CLASS="literal" >__set_state</TT >, <A HREF="language.oop5.cloning.html" >__clone</A > and <A HREF="language.oop5.autoload.html" >__autoload</A > are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >小心</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.oop5.magic.sleep" ><TT CLASS="literal" >__sleep</TT > and <TT CLASS="literal" >__wakeup</TT ></A ></H2 ><P > <A HREF="function.serialize.html" ><B CLASS="function" >serialize()</B ></A > checks if your class has a function with the magic name <TT CLASS="literal" >__sleep</TT >. If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then <TT CLASS="constant" ><B >NULL</B ></TT > is serialized and E_NOTICE is issued. </P ><P > The intended use of <TT CLASS="literal" >__sleep</TT > is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which do not need to be saved completely. </P ><P > The <TT CLASS="literal" >__sleep</TT > method should return the value to serialize (usually <TT CLASS="literal" >$this</TT >), otherwise <TT CLASS="constant" ><B >NULL</B ></TT > is serialized. </P ><P > Conversely, <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A > checks for the presence of a function with the magic name <TT CLASS="literal" >__wakeup</TT >. If present, this function can reconstruct any resources that the object may have. </P ><P > The intended use of <TT CLASS="literal" >__wakeup</TT > is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6045" ></A ><P ><B >例 19-27. Sleep and wakeup</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Connection </font><font color="#007700">{<br /> </font><font color="#0000BB">protected $link</font><font color="#007700">;<br /> </font><font color="#0000BB">private $server</font><font color="#007700">, </font><font color="#0000BB">$username</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">, </font><font color="#0000BB">$db</font><font color="#007700">;<br /> <br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$server</font><font color="#007700">, </font><font color="#0000BB">$username</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">, </font><font color="#0000BB">$db</font><font color="#007700">)<br /> {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">server </font><font color="#007700">= </font><font color="#0000BB">$server</font><font color="#007700">;<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">username </font><font color="#007700">= </font><font color="#0000BB">$username</font><font color="#007700">;<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">password </font><font color="#007700">= </font><font color="#0000BB">$password</font><font color="#007700">;<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">db </font><font color="#007700">= </font><font color="#0000BB">$db</font><font color="#007700">;<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">connect</font><font color="#007700">();<br /> }<br /> <br /> </font><font color="#0000BB">private </font><font color="#007700">function </font><font color="#0000BB">connect</font><font color="#007700">()<br /> {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">link </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">server</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">username</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">password</font><font color="#007700">);<br /> </font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">db</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">link</font><font color="#007700">);<br /> }<br /> <br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__sleep</font><font color="#007700">()<br /> {<br /> return array(</font><font color="#DD0000">'server'</font><font color="#007700">, </font><font color="#DD0000">'username'</font><font color="#007700">, </font><font color="#DD0000">'password'</font><font color="#007700">, </font><font color="#DD0000">'db'</font><font color="#007700">);<br /> }<br /> <br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__wakeup</font><font color="#007700">()<br /> {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">connect</font><font color="#007700">();<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.oop5.magic.tostring" ><TT CLASS="literal" >__toString</TT ></A ></H2 ><P > The <TT CLASS="literal" >__toString</TT > method allows a class to decide how it will react when it is converted to a string. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6053" ></A ><P ><B >例 19-28. Simple example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Declare a simple class<br /></font><font color="#007700">class </font><font color="#0000BB">TestClass<br /></font><font color="#007700">{<br /> </font><font color="#0000BB">public $foo</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">foo </font><font color="#007700">= </font><font color="#0000BB">$foo</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__toString</font><font color="#007700">() {<br /> return </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">foo</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#0000BB">$class </font><font color="#007700">= new </font><font color="#0000BB">TestClass</font><font color="#007700">(</font><font color="#DD0000">'Hello'</font><font color="#007700">);<br />echo </font><font color="#0000BB">$class</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Hello</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > It is worth noting that before PHP 5.2.0 the <TT CLASS="literal" >__toString</TT > method was only called when it was directly combined with <A HREF="function.echo.html" ><B CLASS="function" >echo()</B ></A > or <A HREF="function.print.html" ><B CLASS="function" >print()</B ></A >. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.oop5.magic.set-state" ><TT CLASS="literal" >__set_state</TT ></A ></H2 ><P > This <A HREF="language.oop5.static.html" >static</A > method is called for classes exported by <A HREF="function.var-export.html" ><B CLASS="function" >var_export()</B ></A > since PHP 5.1.0. </P ><P > The only parameter of this method is an array containing exported properties in the form <TT CLASS="literal" >array('property' => value, ...)</TT >. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.oop5.patterns.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop5.final.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Patterns</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop5.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Final Keyword</TD ></TR ></TABLE ></DIV ></BODY ></HTML >