Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 10075

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Auto_Cart&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Auto_Cart</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</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 &quot;10&quot;
    each time a new Auto_Cart is being made with &quot;<em>new</em>&quot;.
    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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Constructor_Cart&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #0000BB">$item&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br />&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;Shop&nbsp;the&nbsp;same&nbsp;old&nbsp;boring&nbsp;stuff.<br /></span><span style="color: #0000BB">$default_cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">;<br />&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;Shop&nbsp;for&nbsp;real...<br /></span><span style="color: #0000BB">$different_cart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Constructor_Cart</span><span style="color: #007700">(</span><span style="color: #DD0000">"20"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">17</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;constructor&nbsp;of&nbsp;A.&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;a&nbsp;regular&nbsp;function&nbsp;named&nbsp;B&nbsp;in&nbsp;class&nbsp;A.&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;not&nbsp;a&nbsp;constructor&nbsp;in&nbsp;A.&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;will&nbsp;call&nbsp;B()&nbsp;as&nbsp;a&nbsp;constructor<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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&#039;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>