After doing some research, I've managed to make a new module/plugin system for my CMS:
Code: Select all
class Framework {
var $modules = array();
//---
// constructor function
//---
function Framework() {
}
//---
// load_module() will load a module into the framework
// @param object $module_object
//---
function load_module( &$module_object ) {
if ( !is_object( $module_object ) ) {
return false;
}
$module_name = $module_object->identitfier;
$module_object->load_framework_object( &$this );
$this->modules[ $module_name ] = &$module_object;
}
//---
// trigger_event() will trigger an event to all loaded modules or
// selected modules
// @param string $event
// @param mixed $module
//---
function trigger_event( $event, $module='' ) {
if ( empty( $module ) ) {
// trigger event to all loaded modules
foreach( $this->modules as $name=>$object ) {
if ( method_exists( $object, $event ) ) {
$this->modules[ $name ]->$event();
}
}
} else if ( is_array( $module ) ) {
// trigger event to a selected number of modules
foreach( $this->modules as $name=>$object ) {
if ( in_array( $name, $module ) && method_exists( $object, $event ) ) {
$this->modules[ $name ]->$event():
}
}
} else {
// trigger to single module
if ( method_exists( $this->modules[ $name ], $event ) ) {
$this->modules[ $name ]->$event();
}
}
}
}
I haven't tested it and just put it together quickly after reading many many observer and other plugin scripts (mainly the oen by d11wtq), but it should allow me to run different events for different modules, or all modules etc - a module would then look like this:
Code: Select all
class module_menu {
var $identitfier = 'menu';
var $framework_object;
//---
// load_framework_object() will load the main framework object
// so that the module can easily access framework functions etc
//---
function load_framework_object( &$framework_object ) {
$this->framework_object = &$framework_object;
}
//---
// display_content() main event for all modules
//---
function dispaly_content( $foo ) {
echo $foo;
}
}
I currently have a SQL class which will allow me in the future to support more than just MySQL databases, currently if I wanted to run a query on a database tabel I would do:
Code: Select all
global $libsql;
$query = $libsql->query( " SELECT * FROM {SQL_PREFIX}table WHERE foo='bar' " );
but I remember someone saying to me once "you shouldn't use globals as they can be bad and cause unwanted effects" or something like that anyway

- so, how could I give modules access to this SQL database "layer" class I've coded without using "global $libsql" ? Would maybe doing this for the $libsql class work?
and then if I wanted to access libsql functions, such as "query" I could do this below, in a module:
Code: Select all
$query = $this->framework_object->query( " SELECT * FROM {SQL_PREFIX}table WHERE foo='bar' " );
or wouldn't that work?
Regards,