As I stated in the previous section, building a different web page for this sample web site is only a matter of deriving another child class from the "HomeWebPage" (not from the base class), since it has already implemented the business logic required to display a complete web document. So, bearing in mind this concept, below I included the definition of a brand new subclass, which displays the "About Us" section of this fictional web site. Having said that, here's the signature of this child class: class AboutUsWebPage extends HomeWebPage{ As you can see, the above "AboutUsWebPage" class only overrides the initial "buildLinks()" method implemented by its corresponding parent, with the purpose of displaying a few additional secondary links on the browser. However, as you might have guessed, the main advantage of this approach rests with using inheritance to create quickly the different sections of this sample web site. Quite simple, right? Logically, if you're anything like me, you might want to see the signature of the source files that display the respective "Home" and "About Us" sections of this sample web site. Below I listed the definitions of these files, in this way demonstrating how a few inherited classes can be used to build a complete object-oriented web site. Given that, here are the source files in question: (definition for index.php file) <?php try{ // instantiate 'HomeWebPage' class (definition for about.php file) try{ // instantiate 'AboutUsWebPage' class As you can see, building the different sections of this sample web site is a process reduced to including the required parent classes and instantiating the concrete class that displays a particular web page. Of course, in this case the functionality provided by inheritance is actually what makes creating distinct web documents a no-brainer process. Finally, I suggest that you try creating your own web page generator classes, so you can understand more easily the way that inheritance can assist you in building dynamic web pages. Final thoughts In this first installment of the series, I hopefully illustrated how inheritance can be utilized in a helpful fashion to build the different web pages of a sample web site, whose look and feel is nearly the same across its respective sections. However, this instructive journey hasn't ended yet. In the last part I'm going to show you how to create some additional web pages to complete the structure of this fictional web site. Now that I've told you about the topics that will be covered in the final article, I'm sure you won't want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|