<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Constructors and Destructors</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.autoload.html">Autoloading Classes</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</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 class="sect2" id="language.oop5.decon.constructor"> <h3 class="title">Constructor</h3> <div class="methodsynopsis dc-description" id="object.construct"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>__construct</strong></span> ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$args</code></span> [, <span class="methodparam"> <code class="parameter">$...</code></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 class="note"><p><strong class="note">Note</strong>: <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 <span class="function"><strong>parent::__construct()</strong></span> within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private). </span> </p></blockquote> <div class="example" id="example-184"> <p><strong>Example #1 using new unified constructors</strong></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 />class </span><span style="color: #0000BB">OtherSubClass </span><span style="color: #007700">extends </span><span style="color: #0000BB">BaseClass </span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// inherits BaseClass's constructor<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// In BaseClass constructor<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 /><br /></span><span style="color: #FF8000">// In BaseClass constructor<br />// In SubClass constructor<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 /><br /></span><span style="color: #FF8000">// In BaseClass constructor<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">OtherSubClass</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 <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> function for a given class, and the class did not inherit one from a parent 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 <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> which was used for different semantics. </p> <p class="para"> Unlike with other methods, PHP will not generate an <strong><code>E_STRICT</code></strong> level error message when <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> is overridden with different parameters than the parent <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> method has. </p> <p class="para"> As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes. </p> <div class="example" id="example-185"> <p><strong>Example #2 Constructors in namespaced classes</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Foo</span><span style="color: #007700">;<br />class </span><span style="color: #0000BB">Bar </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">Bar</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">// treated as constructor in PHP 5.3.0-5.3.2<br /> // treated as regular method as of PHP 5.3.3<br /> </span><span style="color: #007700">}<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="sect2" id="language.oop5.decon.destructor"> <h3 class="title">Destructor</h3> <div class="methodsynopsis dc-description" id="object.destruct"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>__destruct</strong></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 there are no other references to a particular object, or in any order during the shutdown sequence. </p> <div class="example" id="example-186"> <p><strong>Example #3 Destructor Example</strong></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 <span class="function"><strong>parent::__destruct()</strong></span> in the destructor body. Also like constructors, a child class may inherit the parent's destructor if it does not implement one itself. </p> <p class="para"> The destructor will be called even if script execution is stopped using <span class="function"><a href="function.exit.html" class="function">exit()</a></span>. Calling <span class="function"><a href="function.exit.html" class="function">exit()</a></span> in a destructor will prevent the remaining shutdown routines from executing. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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). </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.autoload.html">Autoloading Classes</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</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>