Page 236 - Beginning PHP 5.3
P. 236

Part II: Learning the Language
                    echo “ < h2 > Consuming an apple... < /h2 > ”;
                    $apple = new Fruit;
                    $apple- > consume();

                    echo “ < h2 > Consuming a grape... < /h2 > ”;
                    $grape = new Grape;
                    $grape- > consume();

                    ? >

                       < /body >

                      < /html >
                   You can see the output from this script in Figure  8 - 6 . Notice how the overridden methods,  peel()  and
                  slice() , are called for the  Grape  object, whereas the parent class ’ s  peel()  and  slice()  methods are
                called for the   Fruit  object.






























                            Figure 8 - 6



                  Preserving the Functionality of the Parent Class

                   Occasionally you want to override the method of a parent class in your child class, but also use some of
                 the functionality that is in the parent class ’ s method. You can do this by calling the parent class ’ s
                 overridden method from within the child class ’ s method. To call an overridden method, you write
                   parent::  before the method name:

                    parent::someMethod();



              198





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