<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Constructors</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Handleiding" HREF="index.html"><LINK REL="UP" TITLE="Classes and Objects (PHP 4)" HREF="language.oop.html"><LINK REL="PREVIOUS" TITLE="extends" HREF="keyword.extends.html"><LINK REL="NEXT" TITLE="::" HREF="keyword.paamayim-nekudotayim.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Handleiding</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="keyword.extends.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Hoofdstuk 13. Classes and Objects (PHP 4)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="keyword.paamayim-nekudotayim.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop.constructor" ><VAR CLASS="literal" >Constructors</VAR ></A ></H1 ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Let op</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > In PHP 3 and PHP 4 constructors behave differently. The PHP 4 semantics are strongly preferred. </P ></TD ></TR ></TABLE ></DIV ><P > Constructors are functions in a class that are automatically called when you create a new instance of a class with <VAR CLASS="literal" >new</VAR >. In PHP 3, a function becomes a constructor when it has the same name as the class. In PHP 4, a function becomes a constructor, when it has the same name as the class it is defined in - the difference is subtle, but crucial (see below). </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4757" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Works in PHP 3 and PHP 4.<br /></font><font color="#007700">class </font><font color="#0000BB">Auto_Cart </font><font color="#007700">extends </font><font color="#0000BB">Cart </font><font color="#007700">{<br /> function </font><font color="#0000BB">Auto_Cart</font><font color="#007700">() {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > 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 "new". 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" ><P ></P ><A NAME="AEN4760" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Works in PHP 3 and PHP 4.<br /></font><font color="#007700">class </font><font color="#0000BB">Constructor_Cart </font><font color="#007700">extends </font><font color="#0000BB">Cart </font><font color="#007700">{<br /> function </font><font color="#0000BB">Constructor_Cart</font><font color="#007700">(</font><font color="#0000BB">$item </font><font color="#007700">= </font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">$num </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">add_item </font><font color="#007700">(</font><font color="#0000BB">$item</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">);<br /> }<br />}<br /> <br /></font><font color="#FF8000">// Shop the same old boring stuff.<br /> <br /></font><font color="#0000BB">$default_cart </font><font color="#007700">= new </font><font color="#0000BB">Constructor_Cart</font><font color="#007700">;<br /> <br /></font><font color="#FF8000">// Shop for real...<br /> <br /></font><font color="#0000BB">$different_cart </font><font color="#007700">= new </font><font color="#0000BB">Constructor_Cart</font><font color="#007700">(</font><font color="#DD0000">"20"</font><font color="#007700">, </font><font color="#0000BB">17</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > You also can use the <VAR CLASS="literal" >@</VAR > operator to <SPAN CLASS="emphasis" ><I CLASS="emphasis" >mute</I ></SPAN > errors occurring in the constructor, e.g. <VAR CLASS="literal" >@new</VAR >. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Let op</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > In PHP 3, derived classes and constructors have a number of limitations. The following examples should be read carefully to understand these limitations. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4768" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">A </font><font color="#007700">{<br /> function </font><font color="#0000BB">A</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"I am the constructor of A.<br />\n"</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">B </font><font color="#007700">extends </font><font color="#0000BB">A </font><font color="#007700">{<br /> function </font><font color="#0000BB">C</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"I am a regular function.<br />\n"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">// no constructor is being called in PHP 3.<br /></font><font color="#0000BB">$b </font><font color="#007700">= new </font><font color="#0000BB">B</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > In PHP 3, no constructor is being called in the above example. The rule in PHP 3 is: 'A constructor is a function of the same name as the class.'. The name of the class is B, and there is no function called B() in class B. Nothing happens. </P ><P > This is fixed in PHP 4 by introducing another rule: If a class has no constructor, the constructor of the base class is being called, if it exists. The above example would have printed 'I am the constructor of A.<br />' in PHP 4. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4772" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">A<br /></font><font color="#007700">{<br /> function </font><font color="#0000BB">A</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"I am the constructor of A.<br />\n"</font><font color="#007700">;<br /> }<br /><br /> function </font><font color="#0000BB">B</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"I am a regular function named B in class A.<br />\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"I am not a constructor in A.<br />\n"</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">B </font><font color="#007700">extends </font><font color="#0000BB">A<br /></font><font color="#007700">{<br /> function </font><font color="#0000BB">C</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"I am a regular function.<br />\n"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">// This will call B() as a constructor.<br /></font><font color="#0000BB">$b </font><font color="#007700">= new </font><font color="#0000BB">B</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > In PHP 3, the function B() in class A will suddenly become a constructor in class B, although it was never intended to be. The rule in PHP 3 is: 'A constructor is a function of the same name as the class.'. PHP 3 does not care if the function is being defined in class B, or if it has been inherited. </P ><P > This is fixed in PHP 4 by modifying the rule to: 'A constructor is a function of the same name as the class it is being defined in.'. Thus in PHP 4, the class B would have no constructor function of its own and the constructor of the base class would have been called, printing 'I am the constructor of A.<br />'. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Let op</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Neither PHP 3 nor PHP 4 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 ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Opmerking: </B > There are no destructors in PHP 3 or PHP 4. You may use <A HREF="function.register-shutdown-function.html" ><B CLASS="function" >register_shutdown_function()</B ></A > instead to simulate most effects of destructors. </P ></BLOCKQUOTE ></DIV ><P > Destructors are functions that are called automatically when an object is destroyed, either with <A HREF="function.unset.html" ><B CLASS="function" >unset()</B ></A > or by simply going out of scope. There are no destructors in PHP. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="keyword.extends.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Begin</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="keyword.paamayim-nekudotayim.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><VAR CLASS="literal" >extends</VAR ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop.html" ACCESSKEY="U" >Omhoog</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><VAR CLASS="literal" >::</VAR ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >