PHP DevCenter
oreilly.comSafari Books Online.Conferences.


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.

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: