📄 language.oop5.magic.html
字号:
<!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 http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.patterns.html">Patterns</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.final.html">Final Keyword</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects (PHP 5)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.oop5.magic" class="sect1"> <h2 class="title">Magic Methods</h2> <p class="para"> The function names <i>__construct</i>, <i>__destruct</i> (see <a href="language.oop5.decon.html" class="link">Constructors and Destructors</a>), <i>__call</i>, <i>__callStatic</i>, <i>__get</i>, <i>__set</i>, <i>__isset</i>, <i>__unset</i> (see <a href="language.oop5.overloading.html" class="link">Overloading</a>), <i>__sleep</i>, <i>__wakeup</i>, <i>__toString</i>, <i>__set_state</i> and <a href="language.oop5.cloning.html" class="link">__clone</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"><b class="caution">Caution</b> <p class="simpara"> 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> </div> <div id="language.oop5.magic.sleep" class="sect2"> <h3 class="title"><i>__sleep</i> and <i>__wakeup</i></h3> <p class="para"> <a href="function.serialize.html" class="function">serialize()</a> checks if your class has a function with the magic name <i>__sleep</i>. 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 <b><tt>NULL</tt></b> is serialized and E_NOTICE is issued. </p> <p class="para"> The intended use of <i>__sleep</i> 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 class="para"> Conversely, <a href="function.unserialize.html" class="function">unserialize()</a> checks for the presence of a function with the magic name <i>__wakeup</i>. If present, this function can reconstruct any resources that the object may have. </p> <p class="para"> The intended use of <i>__wakeup</i> is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks. </p> <div class="example"> <p><b>Example #1 Sleep and wakeup</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Connection </span><span style="color: #007700">{<br /> protected </span><span style="color: #0000BB">$link</span><span style="color: #007700">;<br /> private </span><span style="color: #0000BB">$server</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">;<br /> <br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">server </span><span style="color: #007700">= </span><span style="color: #0000BB">$server</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">password </span><span style="color: #007700">= </span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">db </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br /> }<br /> <br /> private function </span><span style="color: #0000BB">connect</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">server</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">username</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">password</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">db</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">link</span><span style="color: #007700">);<br /> }<br /> <br /> public function </span><span style="color: #0000BB">__sleep</span><span style="color: #007700">()<br /> {<br /> return array(</span><span style="color: #DD0000">'server'</span><span style="color: #007700">, </span><span style="color: #DD0000">'username'</span><span style="color: #007700">, </span><span style="color: #DD0000">'password'</span><span style="color: #007700">, </span><span style="color: #DD0000">'db'</span><span style="color: #007700">);<br /> }<br /> <br /> public function </span><span style="color: #0000BB">__wakeup</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -