HomePHP Page 2 - Building Object-Oriented Web Pages with Inheritance in PHP 5
Establishing a basic class hierarchy - PHP
You have probably used the principles of inheritance in any number of your object-oriented programming projects. Traditionally this means working with parent and child classes during the creation of a PHP application. In this two-part series, you'll learn a simple way to use inheritance while creating object-oriented web pages.
Since the main goal of this series is to illustrate how inheritance can be used to build an object-oriented web site, I'm going to define a base web page class, which will be tasked with setting up the blueprints for all the child objects derived from it.
This being said, the corresponding signature for this brand new web page class is listed below:
// define abstract 'WebPage' class abstract class WebPage{ private $html; private $title; private $mainLinks; private $content; abstract public function __construct($title,$content); abstract public function buildMetaData(); abstract public function buildStyles(); abstract public function buildHeader(); abstract public function buildLinks(); abstract public function buildBody(); abstract public function buildFooter(); }
As shown above, the "WebPage" class has been defined as abstract, since it only establishes a generic model of how to build a simple web page. This is clearly reflected by the abstract methods declared by the class in question.
Besides, it's worthwhile to mention here that the previous base class presents a very straightforward structure for outlining the generic characteristics of a basic web document. Nonetheless, as you'll see in a few moments, all the child classes created from this parent will implement concretely all of its methods, in this way allowing the creation of a specific web page.
So far, so good right? At this point you learned how to build a base web page class, which simply behaves as an interface for generating specific web documents. Thus, considering the fact that you may want to see how a subclass derived from the previous parent "WebPage" can be provided with the required business logic to create a concrete web document, in the section to come I'm going to explain this procedure in detail.
So, want to learn how this brand new child class will be defined? Okay, click on the link below and keep reading.