<HTML ><HEAD ><TITLE >Classes and Objects</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Variable functions" HREF="functions.variable-functions.html"><LINK REL="NEXT" TITLE="extends" HREF="keyword.extends.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="functions.variable-functions.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="keyword.extends.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.oop" >Chapter 14. Classes and Objects</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.oop.html#keyword.class" ><TT CLASS="literal" >class</TT ></A ></DT ><DT ><A HREF="keyword.extends.html" ><TT CLASS="literal" >extends</TT ></A ></DT ><DT ><A HREF="language.oop.constructor.html" ><TT CLASS="literal" >Constructors</TT ></A ></DT ><DT ><A HREF="keyword.paamayim-nekudotayim.html" ><TT CLASS="literal" >::</TT ></A ></DT ><DT ><A HREF="keyword.parent.html" ><TT CLASS="literal" >parent</TT ></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 <TT CLASS="literal" >__sleep</TT > and <TT CLASS="literal" >__wakeup</TT ></A ></DT ><DT ><A HREF="language.oop.newref.html" >References inside the constructor</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="keyword.class" ></A ><TT CLASS="literal" >class</TT ></H1 ><P > A class is a collection of variables and functions working with these variables. A class is defined using the following syntax: <DIV CLASS="informalexample" ><A NAME="AEN5433" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Cart { var $items; // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?></PRE ></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="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Caution</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > The following cautionary notes are valid for PHP 4. </P ><P > The name <TT CLASS="literal" >stdClass</TT > is used interally by Zend and is reserved. You cannot have a class named <TT CLASS="literal" >stdClass</TT > in PHP. </P ><P > The function names <TT CLASS="literal" >__sleep</TT > and <TT CLASS="literal" >__wakeup</TT > 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 ><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 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > In PHP 4, only constant initializers for <TT CLASS="literal" >var</TT > 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" ><A NAME="AEN5448" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* None of these will work in PHP 4. */ class Cart { var $todays_date = date("Y-m-d"); var $name = $firstname; var $owner = 'Fred ' . 'Jones'; var $items = array("VCR", "TV"); } /* This is how it should be done. */ class Cart { var $todays_date; var $name; var $owner; var $items; function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></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 <TT CLASS="literal" >new</TT > operator. </P ><DIV CLASS="informalexample" ><A NAME="AEN5452" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $cart = new Cart; $cart->add_item("10", 1); $another_cart = new Cart; $another_cart->add_item("0815", 3);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P > This creates the objects $cart and $another_cart, both of the class Cart. The function add_item() of the $cart object is being called to add 1 item of article number 10 to the $cart. 3 items of article number 0815 are being added to $another_cart. </P ><P > Both, $cart and $another_cart, 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 README.TXT, 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 ->. Thus, the names $cart->items and $another_cart->items name two different variables. Note that the variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign. </P ><DIV CLASS="informalexample" ><A NAME="AEN5456" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// correct, single $ $cart->items = array("10" => 1); // invalid, because $cart->$items becomes $cart->"" $cart->$items = array("10" => 1); // correct, but may or may not be what was intended: // $cart->$myvar becomes $cart->items $myvar = 'items'; $cart->$myvar = array("10" => 1);</PRE ></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 $cart or $another_cart later. Thus, you cannot write $cart->items 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 $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can be read as 'add $num to the $artnr counter of my own items array' or 'add $num to the $artnr counter of the items array within the current object'. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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.variable-functions.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="keyword.extends.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Variable functions</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >extends</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >