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

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




Recommended for You

Tagged Articles

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

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • More O'Reilly Sites
  • igniteshow.com
  • makerfaire.com
  • makezine.com
  • craftzine.com
  • labs.oreilly.com
  • Partner Sites
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com