Basing One Class on Another: Inheritance


It's often useful to base one class on another. For example, say that you've spent a lot of time developing a class named Vehicle that has all kinds of built-in methods that a vehicle might perform, such as start, run, steer, and stop. Now say that you want to create some other classes that are specific types of vehiclesCar, TRuck, Helicopter, Oceanliner, and more.

You could rewrite the Vehicle class's start, run, steer, and stop methods in each of these new classes, or you could base those new classes on the Vehicle class through a process called inheritance. Using inheritance, the properties and methods of the base class (Animal) also become the properties and methods of the derived class(es) (Car, truck, Helicopter, Oceanliner, and so on). Then you can customize the derived class(es) by adding your own properties and methods.

Here's an example; in this case, we'll derive a new class, Lion, from the Animal class. The Lion class will inherit all the properties and methods of the Animal classand to customize the Lion class, we'll add a new method: roar. Obviously, the roar method wouldn't fit some types of Animals (goldfish don't roar), but it's an appropriate method to add to a class named Lion.

To derive one class from another, you use the extends keyword when declaring the derived class, indicating which case class you're extending. You can see this at work in phpinheritance.php, Example 7-2, where we're creating the Lion class by extending Animal and adding the roar method.

After creating an object of the new Lion class, we can use not only its roar method, but also the set_name method it inherited from the Animal class. It's all shown in Example 7-2.

Example 7-2. Using inheritance, phpinheritance.php
 <HTML>     <HEAD>         <TITLE>Using constructors to initialize objects</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using constructors to initialize objects             </H1>             <?php                 class Animal                 {                     var $name;                     function set_name($text)                     {                         $this->name = $text;                     }                     function get_name()                     {                         return $this->name;                     }                 }                 class Lion extends Animal                 {                     var $name;                     function roar()                     {                         echo $this->name, " is roaring!<BR>";                     }                 }                 echo "Creating your new lion...<BR>";                 $lion = new Lion;                 $lion->set_name("Leo");                 $lion->roar();             ?>         </CENTER>     </BODY> </HTML> 

The results appear in Figure 7-2. Very cool.

Figure 7-2. Class inheritance.


Using inheritance, you can reuse the functionality of one class in othersvery useful.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net