Click to See Complete Forum and Search --> : Question of clean code


Dev_M!nX
06-13-2007, 08:15 AM
I have an idea for keeping code nice and clean

I create a class for each PHP-Page that has a template like this
( there's a lot more to this, but this is a rundown of the idea behind it )

filename is class.indexpage.php

<?php

class indexPage
{
public function getName() { return __class__; }
public function __construct();
{
$this->perform(); // this first for all pre-html-rendering functions
$this->render(); // this second to render the page
}
/*
public function load()
{
// this being the index page has the perform and render functions...
// ...being called from the __construct()
// if it was any other page, both functions would be called manually...
// ...by a load() function like this
// example:
$this->perform();
$this->render();
}
*/

private function perform()
{
// place all functions here that could effectively fail or break
// allowing the functions to fail or break before rendering the actual page
// so that the page doesn't fail somewhere in the middle of execution
}

private function render()
{
// place the html/PHP rendering side here
// only render the page here, so that you don't call mysql statements
// or general functions that fail in the middle of the script
// this way, you avoid the page stopping half-way because of errors
?>
<html>
<head>
<!-- Header stuff here -->
</head>
<body>
<!-- body stuff here -->
<body>
</html>
<?php
}
}

?>


i added the getName() function just for the purpose of being able to add all the
pages and classes to a single array (or class), allowing for them to be called from a variable
that can be passed through the scripts/functionCalls as a parameter.

filename is index.php


function __autoload( $class_name )
{
require_once( 'class.'.strtolower( $class_name ).'.php' );
}

new indexPage();



is this ok for keeping the failable/breakable functions away from the page render ?
and being able to Cleanly automate the process of adding new pages to the site.

...later an exception class of some kind will be created
this will be called as a function on any fail :)...

critic is appreciated :)

Regards,
Dev_M!nX

ShawnK
06-13-2007, 05:34 PM
You need to define your variables as private, public, protected, etc...or maybe you just excluded them from the snipp you showed.

Dev_M!nX
06-14-2007, 07:47 AM
which variables ? the functions are defined as public, private, etc.
variables will be declared public, private, etc ... if they are static.
temporary variables won't need to be declared unless passed from the perform to the render :)

ShawnK
06-15-2007, 03:08 AM
Well I can't really tell because you don't show any meaningful code but you can call variables like $class->var...if its marked private or protected you can't do that unless your inside the class itself, in which case you call it as $this->var