The introduction
Today, often enough to develop sites using the language PHP. In the last fifth version of PHP has greatly improved support for
OOP (Object-Oriented Programming).
However, many programmers to create sites using only the most primitive features OOP
such as data encapsulation. Certainly such a use OOP makes the code more qualitative, but applying the OOP and other features can achieve better results.
The use of inheritance can significantly reduce the code, while making it more reliable. Also, this code can often be reused
Home page
We will make a page for a fictional person.
At the top of the page should be a big sign "Home page Name" (usually the logo of the site.)
The following is a menu consisting of the following sections: Home, Biography, Reference.
By the middle of the page is the text of partition.
Below is duplicated for the convenience of the menu.
The site will consist of four main files:
File name |
Appointment |
index.php |
Home Page |
bio.php |
A page with a biography |
links.php |
A page with links |
html.php |
Auxiliary file |
In the auxiliary file, we will keep all of our classes.
Frame site
First of all definable with the fact that the page we will be a class.
We define an abstract class HTML-pages in the file html.php:
Let us look for what each of the methods:
The name of the method |
Purpose of the method |
function __construct($Title) |
To create and initialize an object (in this case, setting
the name of the page). |
function BeginHTML() |
The output header html-file. |
function EndHTML() |
The output end of the html-file. |
function Logo() |
Derivation of the site logo. |
function Menu() |
The output of the main menu. |
abstract function MainText() |
Derivation of the basic content of the web page. |
function Write() |
The output web page, use the methods to display the individual
elements of web pages. |
Some of the methods used to display the individual page elements such as menus, logos and so on. In the Write method, all these functions are called in order to display the entire page. Particular attention should be paid to the abstract method MainText.
This method is abstract because it is not implemented in this class, but only declared. This method will be overridden and implemented in child classes. So on the page links in this method, the link will be displayed, and the biography page - text of the biography by.
The class itself is declared as abstract, and therefore will not create an instance of this class.
The class variable is declared with the $ Title scope protected, you have access to it can get either the class itself or its successors.
Now we have to create the other three files. Show how this can be done on the example of index.php:
In this case, simply create a new class of IndexPage, derived from the class and override the method HTMLPage MainText to display the main content of the page.
Results
The advantages of using OOP will be greater, the greater will be the site. Moreover, it is imperative on the fly, the requirements of the site will be allowed to vary. For example, you might want to add a new page.
To do this will only create a new file with a class derived from HTMLPage, MainText override and create a menu item. Here's how to use inheritance.
It is also easy to change the design of all pages - all changes will occur in the classroom HTMLPage, other pages unasledut design automatically.