Beginning Object Oriented Programming in PHP - Extends
(Page 5 of 6 )
So far our example is only capable of telling us what its content is. We could add more methods to our class, but what if we were building extensions to someone else's class? We don't have to create an entirely new class to add new functionality... we can build a small extension class based on the original.
<?php ; include("class.Box.php");
class ShoeBox extends Box { varr $size;
function ShoeBox($contents, $size) { $this->contents = $contents; $this->size = $size; }
function get_shoe_size() { return $this->size; } }
$mybox = new ShoeBox("Shoes", 10); echo $mybox->get_whats_inside(); echo $mybox->get_shoe_size(); ?> |
With the extends keyword, our script now has access to a ShoeBox class which is based on our original Box class. ShoeBox has all of the same functionality as Box class but also has extra functions specific to a special kind of box.
The ability to write such modular additions to your code gives great flexibility in testing out new methods and saves time by reusing the same core code.
<?php include("class.Box.php"); include("extentions.Shoe.php"); include("extentions.Suggestion.php"); include("extentions.Cardboard.php");
$mybox = new ShoeBox("Shoes", 10); $mySuggestion = new SuggestionBox("Complaints"); $myCardboard = new CardboardBox('', "corrugated", "18in", "12in", "10in"); ?> |
Next: Conclusion >>
More Programming Basics Articles
More By bluephoenix
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|