PHP Manual
Prev |  | Next

# Chapter 13. Classes and Objects

# `class`

A class is a collection of variables and functions working with these variables. A class is defined using the following syntax:

      1
      2 <?php
      3 class Cart {
      4     var $items;  // Items in our shopping cart
      5
      6     // Add $num articles of $artnr to the cart
      7
      8     function add_item ($artnr, $num) {
      9         $this->items[$artnr] += $num;
     10     }
     11
     12     // Take $num articles of $artnr out of the cart
     13
     14     function remove_item ($artnr, $num) {
     15         if ($this->items[$artnr] > $num) {
     16             $this->items[$artnr] -= $num;
     17             return true;
     18         } else {
     19             return false;
     20         }
     21     }
     22 }
     23 ?>
     24

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.

Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with the new operator.

      1
      2  $cart = new Cart;
      3  $cart->add_item("10", 1);
      4

This creates an object $cart of the class Cart. The function add_item() of that object is being called to add 1 item of article number 10 to the cart.

Classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class and what you add in the extended definition. This is done using the extends keyword. Multiple inheritance is not supported.

      1
      2 class Named_Cart extends Cart {
      3     var $owner;
      4
      5     function set_owner ($name) {
      6         $this->owner = $name;
      7     }
      8 }
      9

This defines a class Named_Cart that has all variables and functions of Cart plus an additional variable $owner and an additional function set_owner(). You create a named cart the usual way and can now set and get the carts owner. You can still use normal cart functions on named carts:

      1
      2 $ncart = new Named_Cart;    // Create a named cart
      3 $ncart->set_owner ("kris"); // Name that cart
      4 print $ncart->owner;        // print the cart owners name
      5 $ncart->add_item ("10", 1); // (inherited functionality from cart)
      6

Within functions of a class the variable $this means this object. You have to use $this->something to access any variable or function named something within your current object.

Constructors are functions in a class that are automatically called when you create a new instance of a class. A function becomes a constructor when it has the same name as the class.

      1
      2 class Auto_Cart extends Cart {
      3     function Auto_Cart () {
      4         $this->add_item ("10", 1);
      5     }
      6 }
      7

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 also take arguments and these arguments can be optional, which makes them much more useful.

      1
      2 class Constructor_Cart extends Cart {
      3     function Constructor_Cart ($item = "10", $num = 1) {
      4         $this->add_item ($item, $num);
      5     }
      6 }
      7
      8 // Shop the same old boring stuff.
      9
     10 $default_cart   = new Constructor_Cart;
     11
     12 // Shop for real...
     13
     14 $different_cart = new Constructor_Cart ("20", 17);
     15

Caution
For derived classes, the constructor of the parent class is not automatically called when the derived class's constructor is called.

Prev | Home | Next
Variable functions | Up | Features