Page 235 - Beginning PHP 5.3
P. 235

Chapter 8: Objects
                             $parentObj = new ParentClass;
                             $parentObj- > someMethod();  // Calls ParentClass::someMethod()
                             $childObj = new ChildClass;

                             $childObj- > someMethod();   // Calls ChildClass::someMethod()
                           Notice that the parent class ’ s method is called when accessed from an object of the parent class, and the
                         child class ’ s method is called when using an object of the child class.

                           The following example code shows how you can use inheritance to distinguish grapes from other fruit:
                               < !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
                               < html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
                                < head >
                                  < title > Overriding Methods in the Parent Class < /title >
                                  < link rel=”stylesheet” type=”text/css” href=”common.css” / >
                                < /head >
                                < body >
                                  < h1 > Overriding Methods in the Parent Class < /h1 >

                               < ?php

                             class Fruit {
                               public function peel() {
                                 echo “ < p > I’m peeling the fruit... < /p > ”;
                               }

                               public function slice() {
                                 echo “ < p > I’m slicing the fruit... < /p > ”;
                               }

                               public function eat() {
                                 echo “ < p > I’m eating the fruit. Yummy! < /p > ”;
                               }

                               public function consume() {
                                 $this- > peel();
                                 $this- > slice();
                                 $this- > eat();
                               }
                             }

                             class Grape extends Fruit {
                               public function peel() {
                                 echo “ < p > No need to peel a grape! < /p > ”;
                               }

                               public function slice() {
                                 echo “ < p > No need to slice a grape! < /p > ”;
                               }
                             }






                                                                                                         197





                                                                                                      9/21/09   9:03:42 AM
          c08.indd   197
          c08.indd   197                                                                              9/21/09   9:03:42 AM
   230   231   232   233   234   235   236   237   238   239   240