OOP design (pattern)
Posted: Sun Jan 04, 2004 10:50 am
Hi all, hope you all had a very nice christmas and that 2004 finally may be the year where all your great projects will be realized
As I started learning a bit more about OOP almost one month ago I have red a lot about this subject and come accross a lot of different example scripts and frameworks.
One of those (phpframe) kept my attention because it did approximately what I want.
I hacked a bit the original idea and came up with something that works. But now I'm wondering if this is a good way to go.
Well thx if you red until here and hopefully you will even read the rest.
[File: class.backend.php] the backend class will be included in all other classes such as my database class to be able to use the error functions and to use the config vars.
[file: class.frontend.php] will be used to construct some classes by default and have general functions for pages that output to the browser.
So the idea is that I:
> construct my mysql and template class by default
> can construct new classes when needed (e.g. class.users.php will be loaded when authentication is necessary).
> all my global vars are in the same file (withou using "global" because insecure and without using "Constants" because slow).
> can use the same framework for all my projects => just construct the classes needed.
Plz note that I'm only learning OOP so if this makes no sense it's now that I should know it so just go ahead and burn me to the ground. (with appropriate explanation why if possible
).
thx if you red this whole junk of code and naturally thx 1M times if you post your comments.
cheers
Siech
As I started learning a bit more about OOP almost one month ago I have red a lot about this subject and come accross a lot of different example scripts and frameworks.
One of those (phpframe) kept my attention because it did approximately what I want.
I hacked a bit the original idea and came up with something that works. But now I'm wondering if this is a good way to go.
Well thx if you red until here and hopefully you will even read the rest.
[File: class.backend.php] the backend class will be included in all other classes such as my database class to be able to use the error functions and to use the config vars.
Code: Select all
<?php
class Config
{
var $error_reporting = 1;
var $dbhost = "localhost"; //MySQL Database Server
var $dbuser = "root"; //MySQL Database User
var $dbpass = "XXX"; //MySQL Database Password
var $dbname = "XXX"; //MySQL Database
//other global vars I want to use in all scripts so all my config vars stay
//together in one file.
}
class Object extends Config
{
function Object()
{
// Grab our arguments
$args = func_get_args();
// Register our destructor
register_shutdown_function(array(&$this, '__destruct'));
// Call our contructor
call_user_func_array(array(&$this, '__construct'), $args[0]);
}
/**
* Abstract Constructor
*/
function __construct()
{
// Do nothing
}
/**
* Abstract Destructor
*/
function __destruct()
{
// Do nothing
}
}
class backend extends Object{
function createObject($_classname, $_params='')
{
//echo $_classname;exit;
// include file if it's not already been included
if (!class_exists($_classname)) require_once("class.".$_classname.".php");
if (!is_array($_params)) $object = new $_classname;
else $object =& new $_classname($_params);
// Assign a reference to the application object to the class so App is always accesable
$object->front =& $this;
return $object;
}
//I have some other functions in here such as error function, ...
}
?>Code: Select all
<?php
require_once("class.backend.php");
class frontend extends backend{
var $start;
function __construct() {
$this->datab = $this->createObject('mysql');
$this->tpl = $this->createObject('template');
}
//some other functions in here used for example by index.php
}
?>> construct my mysql and template class by default
> can construct new classes when needed (e.g. class.users.php will be loaded when authentication is necessary).
> all my global vars are in the same file (withou using "global" because insecure and without using "Constants" because slow).
> can use the same framework for all my projects => just construct the classes needed.
Plz note that I'm only learning OOP so if this makes no sense it's now that I should know it so just go ahead and burn me to the ground. (with appropriate explanation why if possible
thx if you red this whole junk of code and naturally thx 1M times if you post your comments.
cheers
Siech