Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // class that extends abstract method must define all abstract declared methods
- /**
- * Polymorphism describes a pattern in object oriented programming
- * in which classes have different functionality while sharing a common interface.
- * refer to http://net.tutsplus.com/tutorials/php/understanding-and-applying-polymorphism-in-php/
- * http://www.easylearntutorial.com/2012/11/advanced-php-tutorial-what-is-polymorphism.html#.UeUwgkAyagZ
- * OR
- * page 44 of Object oriented Programming with php : PACKT
- */
- abstract class Shape
- {
- private $_color = "black";
- private $_filled = false;
- public function getColor()
- {
- return $this->_color;
- }
- public function setColor( $color )
- {
- $this->_color = $color;
- }
- public function isFilled()
- {
- return $this->_filled;
- }
- public function fill()
- {
- $this->_filled = true;
- }
- public function makeHollow()
- {
- $this->_filled = false;
- }
- abstract public function getArea(); // define this method in any class that extends it
- } // End of abstact class MyAbstract
- 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;
- }
- public function getArea()
- {
- return $this->_height * $this->_width;
- }
- } // End of class Rectangle , extending Shape
- echo "<pre>";
- $r = new Rectangle();
- $r->setWidth(10);
- $r->setHeight(20);
- echo $r->getWidth() . "<br>" ;
- echo $r->getHeight() . "<br>" ;
- echo $r->getColor() . "<br>";
- $r->setColor("Green");
- echo $r->getColor() . "<br>";
- var_dump( $r->isFilled() ) ;
- $r->fill();
- var_dump( $r->isFilled() ) ;
- $r->makeHollow();
- var_dump( $r->isFilled() ) ;
- echo"</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement