PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

PHP Foundations Introduction to PHP Objects, Part 2

by John Coggeshall
08/01/2002

Introduction

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

<?php
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:

<?php
  $mycar = new car();
  $mycar->setColor("Blue");
  $mycar->setHP("406");
  $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




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com