Practical application OOP in PHP5
About what clause{article}
Given clause{article} raschitana on the programmers who are possessing experience of development in PHP5 and familiar with bases OOP. In this clause{article} I shall show,
As really in practice it is possible to apply OOP in PHP5 to creation of sites. The code in this clause{article}, is written with the purpose to show primennenii OOP.
Therefore for brevity, the design of a site is placed directly in an initial code though, certainly, it is better to use patterns. Also because of brevity of clause{article}
I sosredotochus` on a spelling of a code on PHP, therefore design will be practically absent. I enough often vtrechal clauses{articles} about OOP in PHP5,
But all of them carried, any abstract character, and at the best indirectly concerned to creation of sites, in this clause{article} I shall show,
How to use OOP for creation of sites.
Introduction
Today it is enough often for development of sites language PHP is used. In last fifth version in PHP support has considerably improved
OOP (Object-oriented Programming). That nemenee many programmers at creation of sites use only the most primitive opportunities OOP,
For example, inkapsuljaciju the data. Certainly such application OOP does{makes} a code by better, but applying and other opportunities OOP it is possible to achieve the greater effect.
Application of polymorphism and inheritance allow to reduce considerably a code, simultaneously doing{making} by his{its} more reliable. Also such code can be used often repeatedly.
Vasi Pupkin's home page
We shall do{make} Vasi Pupkin's page. At the top of page there should be a big inscription " Vasi Pupkin's Home page " (usually it is a trade mark of a site).
Further follows the menu, consisting of the following sections: the Main page, the Biography, Links. On the middle of page there is a text of the unit.
Below for convenience the menu is duplicated.
The site will consist of four basic files:
Name of a file Purpose{appointment}
index.php The main page
bio.php Page with Vasi Pupkin's biography
links.php Page with links
html.php An auxiliary file
We shall store{keep} all our classes in an auxiliary file.
Skeleton of a site
First of all opredelimja with that, that the page at us will be a class.
Let's define{determine} an abstract class of HTML-page in a file html.php:
abstract class HTMLPage
{
protected $Title = " ";
function __ construct ($Title)
{
$this-> Title = " [Vasi Pupkin's Home page] ". $Title;
}
function BeginHTML ()
{
echo <<<HTML
<html>
<head>
<title> {$this-> Title} </title>
</head>
<body>
HTML;
}
function EndHTML ()
{
echo <<<HTML
</body>
</html>
HTML;
}
function Logo ()
{
echo " <h1> Vasi Pupkin's Home page </h1> ";
}
function Menu ()
{
echo <<<HTML
<table>
<tr>
<td> <a href ='index.php '> the Main page </a> </td>
<td> <a href ='bio.php '> the Biography </a> </td>
<td> <a href ='links.php '> Links </a> </td>
</tr>
</table>
HTML;
}
abstract function MainText ();
function Write ()
{
$this-> BeginHTML ();
$this-> Logo ();
$this-> Menu ();
$this-> MainText ();
$this-> Menu ();
$this-> EndHTML ();
}
}
?>
Let's see for what each of methods is necessary:
The name of a method Purpose{appointment} of a method
function __ construct ($Title) Creation and initialization of object (in our case installation
The name of page).
function BeginHTML () A conclusion heading of a html-file.
function EndHTML () A conclusion of the termination{ending} of a html-file.
function Logo () A conclusion of a trade mark of a site.
function Menu () A conclusion of the main menu of a site.
abstract function MainText () A conclusion of the basic maintenance{contents} of web - page.
function Write () A conclusion of web - page, I use methods for a conclusion separate
Elements of web - page.
The part of methods serves for a conclusion of separate elements of page, such, as the menu, a trade mark and so on. In method Write all these functions are caused to deduce{remove} all page entirely. The special attention should be given abstract method MainText. This method nazvaetsja abstract as he is not realized in this class but only it is declared. This method will be redefined and realized in affiliated classes. So on page of links in this method links, and on page of the biography - the text of the biography of Vasi Pupkin will be deduced{removed}. The class is declared tazhe abstract, accordingly, it will be impossible to create ehkzemljary such class.
In a class the variable $Title with area of visibility protected is declared, that is access to her can receive either a class or his{its} successors.
Now it are necessary to create other three files. I shall show, as it can be made on an example index.php:
<? php
include_once ("html.php");
class IndexPage extends HTMLPage
{
function MainText ()
{
echo " <p> Welcome to Vasi Pupkin's homepage ";
}
}
$Page = new IndexPage (" the Main page ");
$Page-> Write ();
?>
In this case new class IndexPage, a derivative from class HTMLPage is simply created and method MainText for a conclusion of the basic maintenance{contents} of page is redefined. For presentation I shall result the circuit iierarkhii classes
Results
Advantages of use OOP will be the more, than there will be a site more. Besides, necessarily, on a course of job, the requirement to a site will be menjatsja. For example, it can be demanded to add new page. For this purpose it will be necessary to create only a new file with a class a derivative from HTMLPage, to redefine method MainText and to create corresponding item{point} of the menu., as it is possible to use inheritance. Also it will be simple to change design of all pages - all changes will occur in class HTMLPage, other pages unasledut design automatically.

|