Page 239 - Beginning PHP 5.3
P. 239

Chapter 8: Objects
                          Here ’ s how you might use  ShapeInfo  to display the color and size of a square:

                             $mySquare = new Square;
                             $mySquare- > setColor( “green” );
                             $mySquare- > makeHollow();
                             $mySquare- > setSideLength( 3 );
                             $info = new ShapeInfo();
                             $info- > setShape( $mySquare );

                             $info- > showInfo(); // Displays “The shape’s color is green, and its area is 9.”
                           You ’ re probably wondering what this has to do with abstract classes. Well, imagine another programmer
                         comes along and creates a new child class,   Rectangle , based on your  Shape  class:
                              class Rectangle extends Shape {
                               private $_width = 0;
                               private $_height = 0;

                               public function getWidth() {
                                 return $this- > _width;
                               }

                               public function getHeight() {
                                 return $this- > _height;
                               }

                               public function setWidth( $width ) {
                                 $this- > _width = $width;
                               }

                               public function setHeight( $height ) {
                                 $this- > _height = $height;
                               }
                             }

                           Notice anything missing? What happens if you try to use a  Rectangle  object with the  ShapeInfo

                          class ’ s   showInfo()  method?
                             $myRect = new Rectangle;
                             $myRect- > setColor( “yellow” );
                             $myRect- > fill();
                             $myRect- > setWidth( 4 );
                             $myRect- > setHeight( 5 );
                             $info = new ShapeInfo();
                             $info- > setShape( $myRect );
                             $info- > showInfo();

                           The answer is that you get the following error:

                             Call to undefined method Rectangle::getArea()
                           Our intrepid programmer has forgotten to create a  getArea()  method in his  Rectangle  class. Or
                          maybe he didn ’ t realize he was supposed to. After all, how was he to know that   Rectangle  objects
                         needed to work with your   ShapeInfo  class?


                                                                                                         201





          c08.indd   201                                                                              9/21/09   9:03:43 AM
          c08.indd   201
                                                                                                      9/21/09   9:03:43 AM
   234   235   236   237   238   239   240   241   242   243   244