<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Classes and Objects (PHP 4)</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Documentaþia limbajului" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Internal (built-in) functions" HREF="functions.internal.html"><LINK REL="NEXT" TITLE="extends" HREF="keyword.extends.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="chapter" 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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="functions.internal.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="keyword.extends.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.oop" >Cap. 13. Classes and Objects (PHP 4)</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Cuprins</B ></DT ><DT ><A HREF="language.oop.html#keyword.class" ><VAR CLASS="literal" >class</VAR ></A ></DT ><DT ><A HREF="keyword.extends.html" ><VAR CLASS="literal" >extends</VAR ></A ></DT ><DT ><A HREF="language.oop.constructor.html" ><VAR CLASS="literal" >Constructors</VAR ></A ></DT ><DT ><A HREF="keyword.paamayim-nekudotayim.html" ><VAR CLASS="literal" >::</VAR ></A ></DT ><DT ><A HREF="keyword.parent.html" ><VAR CLASS="literal" >parent</VAR ></A ></DT ><DT ><A HREF="language.oop.serialization.html" >Serializing objects - objects in sessions</A ></DT ><DT ><A HREF="language.oop.magic-functions.html" >The magic functions <VAR CLASS="literal" >__sleep</VAR > and <VAR CLASS="literal" >__wakeup</VAR ></A ></DT ><DT ><A HREF="language.oop.newref.html" >References inside the constructor</A ></DT ><DT ><A HREF="language.oop.object-comparison.html" >Comparing objects</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="keyword.class" ><VAR CLASS="literal" >class</VAR ></A ></H1 ><P > A class is a collection of variables and functions working with these variables. A class is defined using the following syntax: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3129" ></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">Cart </font><font color="#007700">{<br /> var </font><font color="#0000BB">$items</font><font color="#007700">; </font><font color="#FF8000">// Items in our shopping cart<br /> <br /> // Add $num articles of $artnr to the cart<br /> <br /> </font><font color="#007700">function </font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#0000BB">$artnr</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] += </font><font color="#0000BB">$num</font><font color="#007700">;<br /> }<br /> <br /> </font><font color="#FF8000">// Take $num articles of $artnr out of the cart<br /> <br /> </font><font color="#007700">function </font><font color="#0000BB">remove_item</font><font color="#007700">(</font><font color="#0000BB">$artnr</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">) {<br /> if (</font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] > </font><font color="#0000BB">$num</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] -= </font><font color="#0000BB">$num</font><font color="#007700">;<br /> return </font><font color="#0000BB">true</font><font color="#007700">;<br /> } else {<br /> return </font><font color="#0000BB">false</font><font color="#007700">;<br /> } <br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > This defines a class named Cart that consists of an associative array of articles in the cart and two functions to add and remove items from this cart. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > You can <SPAN CLASS="emphasis" ><I CLASS="emphasis" >NOT</I ></SPAN > break up a class definition into multiple files, or multiple PHP blocks. The following will not work: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3136" ></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">test </font><font color="#007700">{<br /></font><font color="#0000BB">?><br /><?php<br /> </font><font color="#007700">function </font><font color="#0000BB">test</font><font color="#007700">() {<br /> print </font><font color="#DD0000">'OK'</font><font color="#007700">;<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ></TD ></TR ></TABLE ></DIV ><P > The following cautionary notes are valid for PHP 4. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenþie</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > The name <VAR CLASS="literal" >stdClass</VAR > is used interally by Zend and is reserved. You cannot have a class named <VAR CLASS="literal" >stdClass</VAR > in PHP. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenþie</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > The function names <VAR CLASS="literal" >__sleep</VAR > and <VAR CLASS="literal" >__wakeup</VAR > are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. See below for more information. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenþie</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality. </P ></TD ></TR ></TABLE ></DIV ><P > In PHP 4, only constant initializers for <VAR CLASS="literal" >var</VAR > variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below). </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3151" ></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">Cart </font><font color="#007700">{<br /> </font><font color="#FF8000">/* None of these will work in PHP 4. */<br /> </font><font color="#007700">var </font><font color="#0000BB">$todays_date </font><font color="#007700">= </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d"</font><font color="#007700">);<br /> var </font><font color="#0000BB">$name </font><font color="#007700">= </font><font color="#0000BB">$firstname</font><font color="#007700">;<br /> var </font><font color="#0000BB">$owner </font><font color="#007700">= </font><font color="#DD0000">'Fred ' </font><font color="#007700">. </font><font color="#DD0000">'Jones'</font><font color="#007700">;<br /> </font><font color="#FF8000">/* Arrays containing constant values will, though. */<br /> </font><font color="#007700">var </font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"VCR"</font><font color="#007700">, </font><font color="#DD0000">"TV"</font><font color="#007700">);<br />}<br /><br /></font><font color="#FF8000">/* This is how it should be done. */<br /></font><font color="#007700">class </font><font color="#0000BB">Cart </font><font color="#007700">{<br /> var </font><font color="#0000BB">$todays_date</font><font color="#007700">;<br /> var </font><font color="#0000BB">$name</font><font color="#007700">;<br /> var </font><font color="#0000BB">$owner</font><font color="#007700">;<br /> var </font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"VCR"</font><font color="#007700">, </font><font color="#DD0000">"TV"</font><font color="#007700">);<br /><br /> function </font><font color="#0000BB">Cart</font><font color="#007700">() {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">todays_date </font><font color="#007700">= </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d"</font><font color="#007700">);<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">name </font><font color="#007700">= </font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#DD0000">'firstname'</font><font color="#007700">];<br /> </font><font color="#FF8000">/* etc. . . */<br /> </font><font color="#007700">}<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with the <VAR CLASS="literal" >new</VAR > operator. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3155" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$cart </font><font color="#007700">= new </font><font color="#0000BB">Cart</font><font color="#007700">;<br /></font><font color="#0000BB">$cart</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 /></font><font color="#0000BB">$another_cart </font><font color="#007700">= new </font><font color="#0000BB">Cart</font><font color="#007700">;<br /></font><font color="#0000BB">$another_cart</font><font color="#007700">-></font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#DD0000">"0815"</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > This creates the objects <VAR CLASS="varname" >$cart</VAR > and <VAR CLASS="varname" >$another_cart</VAR >, both of the class Cart. The function add_item() of the <VAR CLASS="varname" >$cart</VAR > object is being called to add 1 item of article number 10 to the <VAR CLASS="varname" >$cart</VAR >. 3 items of article number 0815 are being added to <VAR CLASS="varname" >$another_cart</VAR >. </P ><P > Both, <VAR CLASS="varname" >$cart</VAR > and <VAR CLASS="varname" >$another_cart</VAR >, have functions add_item(), remove_item() and a variable items. These are distinct functions and variables. You can think of the objects as something similar to directories in a filesystem. In a filesystem you can have two different files <TT CLASS="filename" >README.TXT</TT >, as long as they are in different directories. Just like with directories where you'll have to type the full pathname in order to reach each file from the toplevel directory, you have to specify the complete name of the function you want to call: In PHP terms, the toplevel directory would be the global namespace, and the pathname separator would be <VAR CLASS="literal" >-></VAR >. Thus, the names <VAR CLASS="varname" >$cart->items</VAR > and <VAR CLASS="varname" >$another_cart->items</VAR > name two different variables. Note that the variable is named <VAR CLASS="varname" >$cart->items</VAR >, not <VAR CLASS="varname" >$cart->$items</VAR >, that is, a variable name in PHP has only a single dollar sign. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3172" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// correct, single $<br /></font><font color="#0000BB">$cart</font><font color="#007700">-></font><font color="#0000BB">items </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=> </font><font color="#0000BB">1</font><font color="#007700">); <br /><br /></font><font color="#FF8000">// invalid, because $cart->$items becomes $cart->""<br /></font><font color="#0000BB">$cart</font><font color="#007700">-></font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=> </font><font color="#0000BB">1</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// correct, but may or may not be what was intended:<br />// $cart->$myvar becomes $cart->items<br /></font><font color="#0000BB">$myvar </font><font color="#007700">= </font><font color="#DD0000">'items'</font><font color="#007700">;<br /></font><font color="#0000BB">$cart</font><font color="#007700">-></font><font color="#0000BB">$myvar </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=> </font><font color="#0000BB">1</font><font color="#007700">); <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > Within a class definition, you do not know under which name the object will be accessible in your program: at the time the Cart class was written, it was unknown that the object will be named <VAR CLASS="varname" >$cart</VAR > or <VAR CLASS="varname" >$another_cart</VAR > later. Thus, you cannot write <VAR CLASS="varname" >$cart->items</VAR > within the Cart class itself. Instead, in order to be able to access it's own functions and variables from within a class, one can use the pseudo-variable <VAR CLASS="varname" >$this</VAR > which can be read as 'my own' or 'current object'. Thus, '<VAR CLASS="varname" >$this->items[$artnr]</VAR > += <VAR CLASS="varname" >$num</VAR >' can be read as 'add <VAR CLASS="varname" >$num</VAR > to the <VAR CLASS="varname" >$artnr</VAR > counter of my own items array' or 'add <VAR CLASS="varname" >$num</VAR > to the <VAR CLASS="varname" >$artnr</VAR > counter of the items array within the current object'. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > There are some nice functions to handle classes and objects. You might want to take a look at the <A HREF="ref.classobj.html" >Class/Object Functions</A >. </P ></BLOCKQUOTE ></DIV ></DIV ></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="functions.internal.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="keyword.extends.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Internal (built-in) functions</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><VAR CLASS="literal" >extends</VAR ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >