PHP has limited support for object-oriented programming and allows programmers to define their own classes and create object instances of those classes. We make little use of objects in this tutorial, and this section serves as an introduction to PHP's support of object-oriented features. The subject of object-oriented programming is extensive, and we don't provide a complete explanation of the subject here.

Classes and Objects

A class defines a compound data structure made up of member variables and a set of functions-known as methods or member functions-that operate with the specific structure. Example 2-7 shows how a class Counter is defined in PHP. The class Counter contains two member variables-the integers $count and $startPoint-and four functions that use these member variables. Collectively, the variables and the functions are members of the class Counter.

Example 2-7. A simple class definition of the user-defined class Counter
  // A class that defines a counter.
  class Counter
    // Member Variables
    var $count = 0;
    var $startPoint = 0;
    // Methods
    function startCountAt($i)
      $this->count = $i;
      $this->startPoint = $i;
    function increment(  )
    function reset(  )
      $this->count = $this->startPoint;
    function showvalue(  )
      print $this->count;

To use the data structures and functions defined in a class, an instance of the class-an object-needs to be created. Like other data types-integers, strings, arrays, and so on-objects are held by variables. However, unlike other types, objects are created using the new operator. An object of class Counter can be created and assigned to a variable as follows:

$aCounter = new Counter;

Once the variable $aCounter is created, the member variables and functions of the new object can be used. Members of the object, both variables and functions, are accessed using the -> operator. Consider the following example:

echo $aCounter->count;  // prints 0
$aCounter->increment(  );
echo $aCounter->count;  // prints 1
// Bypass the function that updates count
$aCounter->count = 101;

In the class definition, the code that defines member functions can access the member variables with the variable $this as can be seen in the Counter function implementations in Example 2-7. The variable $this has special meaning and acts as a placeholder until a real object is created. For example, when the function $aCounter->increment( ) is called, the variable $this acts as $aCounter.

By placing the code shown in Example 2-7 in the file, the class Counter can be used by other scripts to create new objects, as shown in Example 2-8.

Example 2-8. Creating and using objects of class Counter
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    include "";
    // Create a new object of type "counter"
    $temp = new Counter;
    // Set the counter to 10
    // Increment the counter
   $temp->increment(  );
    // Print out the value of the counter
    echo "<p>Counter is now: ";
    $temp->showvalue(  );
    // Reset the counter
    $temp->reset(  );
    // Print out the value of the counter
    echo "<p>Counter is now: ";
    $temp->showvalue(  );

Many objects of the same class can be created. For example, you can use the following fragment to create three objects and assign them to three variables:

$a = new Counter;
$b = new Counter;
$c = new Counter;

The variables $a->count, $b->count, and $c->count are different. Each variable is of type object and references an object of the class Counter, but the objects themselves are independent.