Published on (
 See this if you're having trouble printing code examples

PHP Foundations Introduction to PHP Objects, Part 2

by John Coggeshall


Welcome back! In my last column I introduced to you the concept of classes in PHP and provided you with some basic examples for writing your scripts in an object-oriented way. This week, I'll discuss the concept of objects in more detail, including some of the more powerful features (and the main reason to use objects) such as inheritance and using objects as data-storage containers. Let's get started.

Object Hierarchies

One of the most powerful uses for objects and classes within PHP (as with any other programming language that supports objects) is the concept of an object hierarchy. Although it is sometimes difficult to apply, the principals are rather easy to explain. Let's say, for example, you are interested in writing a script to catalog types of cars. One approach, of course, would be to use arrays to store all the possible information that could exist. However, this will not work as well for situations where you would also like to store information unique to a specific type of car (such as the towing capacity of a truck). A more efficient and powerful method is to use objects, as I will show you below.

Extending Objects

Before we can really discuss a good solution to the above cataloging problem, first we have to introduce the concept of class extending. Let's take a look at a quick example:

Related Reading

Programming PHP
By Rasmus Lerdorf, Kevin Tatroe

class item {
  var $id;
 function getID() {
   return $this->id;
class car extends item {
  var $color;
  var $horsepower;
  function setColor($color) {
   $this->color = $color;
  function setHP($hp) {
   $this->horsepower = $hp;
  function getHP() {
   return $this->horsepower;
  function getColor() {
   return $this->color;

In the above code, we have defined two classes. The first class, item, has a single member variable $id with a function used to retrieve it getID(). We could create an instance of this class, however, it wouldn't be very useful by itself. To make the creation of the item class useful we must look to the second class we defined: car. As you can see above, the car class has "extended" the item class declared above and has access to all of its functions and member variables. To illustrate this point, examine the following code:

  $mycar = new car();
  $mycar->id = "myidentification";
  echo "The ID of my car is: ".$mycar->getID();

Considering only what we have learned so far in part one of this series, the first three lines of the above code should be fairly self-explanatory. However, looking at our original class definition for the car class, lines four and five seem to refer to variables and functions that are undefined in the class. Because our defined car class extends the item class (called the parent class), it has access to all of its member variables and functions, so the above code will work as expected.

Overloading Member Functions and Parent Referencing

One of the most powerful features of object-oriented programming is the ability to overload a function defined within another class. For instance, adding the following to the class definition of car:

function getID() {
  return "No ID Found";

Any references to the member function getID() from an instance of the car class will result in executing the function defined within the car class. Although useful, there may be times when the functionality of both the initiated object and its parent class is desired (for instance, when the parent class provides a basic functionality to be extended by a child class). To facilitate this, PHP provides the parent reference. Below is an example of the parent reference in action:

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

class parentclass {
  function foo() {
   echo "This is the parent class<BR>";
 class childclass extends parentclass {
  function foo() {
   echo "This is the child class<BR>";
 $myclass = new childclass();

Executing this script produces the following output:

 This is the child class
 This is the parent class

Object Serialization

Along with the active functional aspects of objects in PHP, they can also be stored (in their current state) in a database or a file system through a process called serialization. This process is accomplished through the functions serialize() and unserialize(), which converts an instance of an object (or any variable) into a string that can be written to a file or stored within a database, as shown in the code below:

  class foo {
   var $a = 1;
   function getvar() {
    return $this->a;
   function addvar() {
  $bar = new foo();
  echo "The value of the variable a is: ".$bar->getvar()."<BR>";
$str = serialize($bar);
  $foobar = unserialize($str);
  echo "The value of the variable a is: ".$foobar->getvar()."<BR>";

As the above code demonstrates, first we define and create an instance of the class foo (which we store in the variable $bar) and echo its variable to the browser. Then, we increment the variable in our $bar class and store the serialization string of $bar created by the serialize() function in the $str variable. At this point, note that the instance $bar has been completely destroyed. From this point, we recreate the object from its serialization string and store it into a new variable $foobar, effectively recreating the object from its original state before it was destroyed. Here's what the output will look like:

 The value of the variable a is: 1
 The value of the variable a is: 2

Please note that, when an object is being recreated through a call to the unserialize() function, it is important that the class defining the instance of the object exist. For example, attempting to reconstruct the foo class above, without having the class definition for the foo class, results in an empty, useless class being created.

More to Come

This concludes my discussion (at least for now) of using objects within PHP! I hope you now have a firm handle on how objects are handled within PHP, and that you also have some great tricks at your disposal to create reusable and storable PHP code. In my next article, I'll be tackling a whole new aspect of writing scripts in PHP when I discuss the closer thing that PHP has to a C-style pointer: References.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.

Read more PHP Foundations columns.

Return to the PHP DevCenter.

Copyright © 2009 O'Reilly Media, Inc.