Skip Navigation LinksHome > Categories > Code from a Category

Practical application of OOP in PHP5

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1216
Add Date: 03/02/2012
This article is designed for programmers who have experience developing in PHP5 and familiar with the basics of OOP. In this article I will show as a real practice can be used OOP in PHP5 for creating websites. The code in this article was written to show exactly primennenii PLO.Therefore, for the sake of brevity, the design of the site is placed directly in the source code, though, of course, it is better to use templates. Also, because of the brevity of Article I will focus on writing code in PHP, so the design will be practically absent. I often had meetings articles about OOP in PHP5, but they were some kind of abstract, and at best indirectly related to creating websites, in this article, I show how to use the OOP for creating websites.

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.


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.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.