Doloj procedural programming, you allow object-oriented!

Programmers - people lazy. Therefore, when business reaches job, they all over again search for any software product which to some extent satisfies their needs{requirements} for the decision of a task in view in a network. If the programmer writes something on PHP one of the first systems which he will find, will be PHP Nuke. Having played with it  nekotore time, the programmer understands, that a thing, certainly, good, but too "clumsily written, hardly adapted to the problems{tasks}, distinct from a web-portal, and translation into Russian is made by the person, having no more than three glasses on great mighty.


Nevertheless, the approach to storage of a code in that kind as it is realized in PHP Nuke and it is described on a plenty of the sites, devoted PHP, it seems quite logical, accessible and priemlimym.


The general circuit of this campaign named "modular", is those: there Is a certain main script, for example index.php. This script in a certain parameter, for example, $module, accepts a name of the module which gives a browser any page. index.php looks approximately thus:

<?

include "config.php";

if (! $module ||! file_exists (" modules / $ module.inc.php ")) {

$module = "default";

}

include " modules / $ module.inc.php ";

?>



Typical "module" at such construction of a site looks so:

<?

if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}

$page_title = "..."; // we establish{install} a name of page

include "includes/header.php"; // we connect design

// Certain php - a code which generates the given page

include "includes/footer.php"; // we connect the rests of design

?>



Like very well, however there are the some people but:

1.         In kazhom the module it is necessary to do{make} include heading - differently we can not change and other tags in the beginning of page.

2.         In kazhom the module it is necessary to do{make}

if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}


3.         

- That have not called directly, having passed various initialization of variables, connection to SUBD, include the general{common} functions, etc. in config.php. Though, actually, such interdiction is done{made} by three lines in .htaccess:

<files *.php>

deny from all

</files>


4.         In each module it is necessary to do{make} include the bottom part of page - for the same reason, as item 1

5.         At programming modules to have repeatedly to cause the same procedures: generation of the menu, navigation, banerov, votings, etc. Even if all these procedures will be with beautiful, legkozpominaemymi names, with a small amount of parameters, will contain all HTML-formatting inside itself all the same each time at a spelling of the module it will be necessary to write calls of all these procedures consistently:

<?

PageNavigation (module params);

PageLeftMenu (module params);

// A PHP-code generating a content

PageRelatedLinks (module params);

PageNewsLinks (module params);

PageVotes (module params);

PageBanner (module params);

?>


6.         Others but, forgotten as terrible dream


Thus, the spelling of the new module begins with copying the text of the old module with the subsequent correction of the block responsible{crucial} for a content.


However there is a way to relieve from this monotonous, long and to nobody of the necessary process - for this purpose it is necessary to recollect existence OOP (object-oriented programming) and that PHP is very quite good this most OOP supports.


For the beginning we shall create a class - Web-page in which we shall describe all funcii, used at generation of our pages:

class WebPage

{

// If the name of function coincides with a classname she is considered the designer

// Speaking Russian, she is carried out at creation of object

function WebPage ($module)

{

$this-> page_title = "demo - module";

}

function PageHeader ()

{

include "includes/header.inc.php";

// In this file instead of <? = $page_title;?> it will be necessary to write <? = $this-> page_title;?>

}

function PageFooter ()

{

include "includes/footer.inc.php";

}

function PageNavigation ()

{

// A code for navigation

}

function PageLeftMenu ()

{

// A code for the menu in the left part of page

}

function PageContent ()

{

// A code generating a content

}

function PageRelatedLinks ()

{

// The code generating the links to connected sections

}

function PageNewsLinks ()

{

// A code generating the block of news

}

function PageVotes ()

{

// A code generating the block of votings

}

function PageBanner ()

{

// A code generating a banner

}

function Run ()

// kh-n Run consistently causes all necessary

// Methods of class WebPage for construction of page

{

$this-> PageNavigation ();

$this-> PageLeftMenu ();

$this-> PageContent ();

$this-> PageRelatedLinks ();

$this-> PageNewsLinks ();

$this-> PageVotes ();

$this-> PageBanner ();

}

}

?>



Now, if we should make, for example, page which differs otstandartnoj only the block of a content we can define{determine} the new class derivative from WebPage:

class CoolPage extends WebPage

{

// We redefine the designer to change a name of the module

function CoolPage ()

{

// We cause the designer of a parental class - suddenly he useful does{makes} something?;)

parent:: WebPage ();

$this-> page_title = " the Abrupt module ";

}

function PageContent ()

{

// We deduce{remove} a content

}

}



If on page news we determine other class are not necessary for us:

class CoolPageWithoutNews extends CoolPage

{

// Here we do not describe funciju CoolPageWithoutNews

// In this case PHP the designer of a parental class will automatically call

// Accordingly the name of our page will be " the abrupt module "

function PageNewsLinks ()

{

// Here it is empty, that links to news were not deduced{removed}

}

}



And so on.


Basically, if you have a plenty of classes the code of each of too can be placed them in a separate file, however it is necessary to watch{keep up} that that classes which have derivative classes, were accessible always, differently PHP cannot understand, from what file to take a code of a parental class.


Example of a file index.php which can represent itself as " the main file ":

<?

include "config.php";

include "base_classes.php";

if ($module ** file_exists ($file = " modules / $ module.inc.php ")) {

// We check, whether there is a file with "body" of a class

include $file;

}

if (! class_exists ($module)) {

// We check, that the class exists

$module = "WebPage";

}

$page=new $module; // we create object

$page-> Run (); // we start generation of page

}

?>



The note: not looking that all above mentioned code was never started, the above-stated technology, with that difference, that almost the information is in a DB, is really maintained, for example, on my site.