language.oop5.decon.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 107 行
HTML
107 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Constructors and Destructors</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.autoload.html">Autoloading Objects</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.visibility.html">Visibility</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.decon" class="sect1"> <h2 class="title">Constructors and Destructors</h2> <div id="language.oop5.decon.constructor" class="sect2"> <h3 class="title">Constructor</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><b><b>__construct</b></b></span> ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$args</tt></span> [, <span class="methodparam"> <tt class="parameter">$...</tt></span> ]] )</div> <p class="para"> PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to <b>parent::__construct()</b> within the child constructor is required. </span> </p></blockquote> <div class="example"> <p><b>Example #1 using new unified constructors</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">BaseClass </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br /> print </span><span style="color: #DD0000">"In BaseClass constructor\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">SubClass </span><span style="color: #007700">extends </span><span style="color: #0000BB">BaseClass </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">();<br /> print </span><span style="color: #DD0000">"In SubClass constructor\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">BaseClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">SubClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> For backwards compatibility, if PHP 5 cannot find a <b>__construct()</b> function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named <b>__construct()</b> which was used for different semantics. </p> </div> <div id="language.oop5.decon.destructor" class="sect2"> <h3 class="title">Destructor</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><b><b>__destruct</b></b></span> ( <span class="methodparam">void</span> )</div> <p class="para"> PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. </p> <div class="example"> <p><b>Example #2 Destructor Example</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">MyDestructableClass </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br /> print </span><span style="color: #DD0000">"In constructor\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">"MyDestructableClass"</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">__destruct</span><span style="color: #007700">() {<br /> print </span><span style="color: #DD0000">"Destroying " </span><span style="color: #007700">. </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyDestructableClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call <b>parent::__destruct()</b> in the destructor body. </p> <blockquote><p><b class="note">Note</b>: Destructors called during the script shutdown have HTTP headers already sent. The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache). <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error. <br /> </p></blockquote> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.autoload.html">Autoloading Objects</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.visibility.html">Visibility</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></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?