<!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</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="keyword.extends.html">extends</a></div> <div class="next" style="text-align: right; float: right;"><a href="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="oop4.constructor" class="sect1"> <h2 class="title">Constructors</h2> <p class="para"> Constructors are functions in a class that are automatically called when you create a new instance of a class with <em>new</em>. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists. </p> <div class="informalexample"> <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">Auto_Cart </span><span style="color: #007700">extends </span><span style="color: #0000BB">Cart </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">Auto_Cart</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "<em>new</em>". Constructors can take arguments and these arguments can be optional, which makes them much more useful. To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values. </p> <div class="informalexample"> <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">Constructor_Cart </span><span style="color: #007700">extends </span><span style="color: #0000BB">Cart </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #0000BB">$item </span><span style="color: #007700">= </span><span style="color: #DD0000">"10"</span><span style="color: #007700">, </span><span style="color: #0000BB">$num </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">add_item </span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">$num</span><span style="color: #007700">);<br /> }<br />}<br /> <br /></span><span style="color: #FF8000">// Shop the same old boring stuff.<br /></span><span style="color: #0000BB">$default_cart </span><span style="color: #007700">= new </span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">;<br /> <br /></span><span style="color: #FF8000">// Shop for real...<br /></span><span style="color: #0000BB">$different_cart </span><span style="color: #007700">= new </span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #DD0000">"20"</span><span style="color: #007700">, </span><span style="color: #0000BB">17</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> You also can use the <em>@</em> operator to <em class="emphasis">mute</em> errors occurring in the constructor, e.g. <em>@new</em>. </p> <div class="informalexample"> <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">A<br /></span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">A</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">"I am the constructor of A.<br />\n"</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">B</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">"I am a regular function named B in class A.<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"I am not a constructor in A.<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">// This will call B() as a constructor<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> The function B() in class A will suddenly become a constructor in class B, although it was never intended to be. PHP 4 does not care if the function is being defined in class B, or if it has been inherited. </p> <div class="caution"><strong class="caution">Caution</strong> <p class="simpara"> PHP doesn't call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate. </p> </div> <p class="para"> Destructors are functions that are called automatically when an object is destroyed, either with <span class="function"><a href="function.unset.html" class="function">unset()</a></span> or by simply going out of scope. There are no destructors in PHP. You may use <span class="function"><a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a></span> instead to simulate most effects of destructors. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="keyword.extends.html">extends</a></div> <div class="next" style="text-align: right; float: right;"><a href="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>