Pablo,
This is a very complex discussion... But generalizing, a LOT, OO is more appropriated for big systems due to its extensibility and easy maintenance, while procedural approach works best for small applications that don't require to much updates and aren't too complex.
Here is a example of a controller of my framework, can you imagine it using procedural approach? (http://www.yourhostname.com/controller/articles)
<?php /** * Project: BBBM Framework * File: site * * @desc Site Controller * @link http://www.bbbm.com.br/ * @copyright 2004 Bruno B B Magalhaes * @author Bruno B B Magalhaes <brunomagalhaes@xxxxxxxxxxx> * @package BBBM Framework * @version 0.7-dev */ define('FRAMEWORK_DIR',dirname(__FILE__).'/framework/'); require_once(FRAMEWORK_DIR.'framework.inc.php');
/** * Starting Framework */ $framework = new framework();
/**
* Checking if it's a valid and installed Controller
*/
if(isset($framework->input->uri['1']))
{
$framework->benchmark->mark('framework','loading_called_controller');
if(!$framework->load_controller($framework->input->uri['1']))
{
echo '[FRAMEWORK FATAL ERROR] There are no registered or active controllers!';
exit;
}
}
else
{
$framework->benchmark->mark('framework','loading_default_controller');
if(!$framework->load_controller())
{
echo '[FRAMEWORK FATAL ERROR] There are no registered or active controllers!';
exit;
}
}
/**
* Getting all modules from this controller and module information from database
*/
$framework->benchmark->mark('framework','loading_controller_modules');
$framework->modules->get_modules($framework- >controller['controllerPath']);
/**
* Configuring Output (Templates dirs, Compile dirs, etc)
*/
$framework->benchmark->mark('framework','configuring_controller');
$framework->output->configure_controller($framework- >controller['controllerPath']);
/**
* If a controller needs authentication, try to authenticate!
*/
$framework->benchmark->mark('framework','authenticating_controller');
$framework->authentication->authenticate($framework- >controller['controllerPath'],$framework- >controller['controllerLevel']);
if(!$framework->authentication->authenticated)
{
if(isset($framework->authentication->errormsg))
{
$framework->output->assign('errormsg',$framework->authentication- >errormsg);
}
$framework->output->display($framework- >controller['controllerPath'].'.templates/login.tpl',session_id());
$framework->benchmark->stop('framework');
exit;
}
/**
* Getting Module Information, if exists, else get the default module for this controller
*/
if(isset($framework->input->uri['2']))
{
$framework->benchmark->mark('framework','loading_called_module');
$framework->modules->get_module($framework->input->uri['2']);
}
else
{
$framework->benchmark->mark('framework','loading_default_module');
$framework->modules->get_module();
}
/**
* Starting module initialization routines
*/
$framework->benchmark->mark('framework','initializing_module');
if(isset($framework->modules->module) && $framework->modules->module != '' && $framework->modules->module != false && is_array($framework->modules->module))
{
/**
* If a module needs authentication, try to authenticate!
*/
$framework->benchmark->mark('framework','authenticating_module');
if($framework->controller['controllerLevel'] < $framework->modules->module['moduleLevel'])
{
$framework->authentication->authenticate($framework->modules- >module['modulePath'],$framework->modules->module['moduleLevel']);
if(!$framework->authentication->authenticated)
{
if(isset($framework->authentication->errormsg))
{
$framework->output->assign('errormsg',$framework->authentication- >errormsg);
}
$framework->output->display($framework- >controller['controllerPath'].'.templates/login.tpl',session_id());
$framework->benchmark->stop('framework');
}
}
/**
* Checking if the called module is an alias, and if the alias exists and exits after it!
*/
$framework->benchmark- >mark('framework','checking_and_loading_alias_module');
if($framework->modules->module['moduleType'] == 'alias')
{
if(isset($framework->modules->module['moduleAliasPath']) && $framework->modules->module['moduleAliasPath'] != '' && file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.aliases/'.$framework->modules- >module['moduleAliasPath'].'.func.php'))
{
include_once(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.aliases/'.$framework->modules- >module['moduleAliasPath'].'.func.php');
}
elseif(isset($framework->modules->module['modulePath']) && $framework->modules->module['modulePath'] != '' && file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.aliases/'.$framework->modules- >module['modulePath'].'.func.php'))
{
include_once(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.aliases/'.$framework->modules- >module['modulePath'].'.func.php');
}
else
{
echo '[MODULE INITIALIZATION] No module to load!';
}
exit;
}
/**
* Including module
*/
$framework->benchmark->mark('framework','including_module');
if(isset($framework->modules->module['moduleAliasPath']) && $framework->modules->module['moduleAliasPath'] != '' && file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['moduleAliasPath'].'/'.$framework->modules- >module['modulePath'].'.class.php'))
{
require_once(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['moduleAliasPath'].'/'.$framework->modules- >module['modulePath'].'.class.php');
}
elseif(isset($framework->modules->module['modulePath']) && $framework->modules->module['modulePath'] != '' && file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['modulePath'].'/'.$framework->modules- >module['modulePath'].'.class.php'))
{
require_once(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['modulePath'].'/'.$framework->modules- >module['modulePath'].'.class.php');
}
else
{
echo '[MODULE INITIALIZATION] No module to load!';
exit;
}
/**
* Configuring module
*/
$framework->benchmark->mark('framework','configurating_module');
$framework->output->configure_module($framework->modules- >module['modulePath']);
/**
* Running module
*/
$framework->benchmark->mark('framework','starting_module');
if(isset($framework->modules->module['modulePath']) && class_exists($framework->modules->module['modulePath']))
{
$module = new $framework->modules->module['modulePath']();
}
/**
* Running called action or default, with the suplied arguments
*/
if(isset($module) && is_object($module))
{
if(isset($framework->input->uri['3']) && method_exists($module,$framework->input->uri['3']))
{
$framework->benchmark- >mark('framework','loading_module_called_action');
$action = $framework->input->uri['3'];
if(isset($framework->input->uri['4']) && $framework->input->uri['4'] != '')
{
$id = $framework->input->uri['4'];
$module->$action($id);
}
else
{
$module->$action();
}
}
elseif(method_exists($module,'default_action'))
{
$framework->benchmark- >mark('framework','loading_module_default_action');
$module->default_action();
}
else
{
echo '[MODULE INITIALIZATION] No Action to perform!';
exit;
}
}
/**
* Outputing everything
*/
$framework->benchmark->mark('framework','outputing_module');
if(is_object($module) && isset($module->module['template']))
{
if(file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['moduleAliasPath'].'/templates/'.$module- >module['template'].'.tpl'))
{
$framework->output->display($framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['moduleAliasPath'].'/templates/'.$module- >module['template'].'.tpl',session_id());
}
elseif(file_exists(FRAMEWORK_DIR.$framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['modulePath'].'/templates/'.$module- >module['template'].'.tpl'))
{
$framework->output->display($framework- >controller['controllerPath'].'.modules/'.$framework->modules- >module['modulePath'].'/templates/'.$module- >module['template'].'.tpl',session_id());
}
else
{
echo '[MODULE OUTPUT] Output error!';
exit;
}
}
else
{
echo '[MODULE OUTPUT] Output error!';
exit;
}
}
else
{
echo '[FRAMEWORK FATAL ERROR] Unable to start module\'s initialization routines!';
exit;
}
$framework->benchmark->stop('framework');
?>
Best Regards, Bruno B B Magalhaes
On Nov 16, 2004, at 8:13 PM, Pablo Fernandez Stearns wrote:
Hi, This is my first post. English is my 2nd language so I apologize for any mistakes I might have.
I would describe myself as an intermediate self-thaught PHP programmer, which means I can do lots of stuff but probly my coding techniques sucks :P
Anyway, Im reading a lot about OOP lately, and I've fixing other's people scripts which use OOP for "small" stuff that could have been acomplished with simple functions or includes maybe... so I started wondering, why use OOP when you can use something simpler...
Imagine the following example:
A table of MySQL on a simple classifieds site holds the info for each Ad posted ( timestamp, user who posted it, title, description, location, country, etc).
A php page called ads.php gets the ad ID via $_GET and prints out its information in a nicely formated page.
What I usually do is to put all the "echo-ing" part of this page in a separate include (ie: ads_details.php ), so that if I need to also show this in another page, I call the same include. If I decide to do a change later, I only have to change it in one part of the script alone.
What I've seen a lot lately is that people do the same with classes (they write a class that will output the same thing I output thru an include)... so I guess there must be pros and cons regarding using both methods...
Could anyone point me to a good reading (free, internet) material to read about this or any other advice so I continue learning about OOP and when to use it ?
If you read it all the way down here THANKS ! :D
Pablo Fernandez-Stearns BA. Argentina.
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php