Framework SuperObject

The Framework SuperObject is a simple, lightweight and extensible framework object I've developed initially for use at YSTV, however there is no reason it cannot be used as a basis for any PHP-based site.

Getting Framework SuperObject

Currently all of the source for FSO can be found at http://svn.codescape.net/ystvfso/trunk.

Using the FSO object

<?php
//  Load the FSO class
require_once('/path/to/fso.php');
//  Get a reference to the FSO object
$fso =& FSO::get_instance();
//  ... or ...
$fso =& get_instance();
?>

You can then get to any loaded module by using

<?php
$fso->mymodule;
?>

Loading and accessing modules

You can load modules in one of several ways:

<?php
//  Load module 'MyModule' at $fso->mymodule
FSO::load_module('MyModule');
//  Load module with arguments to the __init() method
FSO::load_module('MyModule', array('arg1', 'arg2'));
//  Load module 'MyModule' at $fso->foo
FSO::load_module('MyModule', null, 'foo');
//  Load module 'MyModule' from a different path
FSO::load_module('MyModule', null, null, '/path/to/module/dir');

//  Or load multiple modules
FSO::load_modules(array(
    array('MyModule', null, 'myalias'),
    array('AnotherModule', array('init', 'args', 'are', 'fun'))
));
?>

Modules can also be accessed in many ways:

<?php
//  When loading the module
$mymodule =& FSO::load_module('MyModule');
//  From the FSO object
$fso =& FSO::get_instance();
$fso->mymodule->somefunction();
//  Reference the module with another variable
$mymodule =& $fso->mymodule;
//  ... or ...
$mymodule =& FSO::get_module('mymodule');
?>

You can access any module via any other module:

<?php
$mymodule->othermodule->somefunction();
?>

Finally, if necessary you can load a module without actually making it part of the superobject, however it will still have access to the rest of FSO via $this->module:

<?php
$mymodule =& FSO::make_module('MyModule', array('some', 'args'));
?>

You can access any module from within any other module by using $this->mymodule. If you have object variables which might interfere with this, you can also access other modules with $this->FSO->mymodule.

Note: overloading __get() will stop $this->mymodule and $mymodule->othermodule types of access from working. You will still be able to use $this->FSO->mymodule.

Adding modules

To create a module for FSO, create a file with the same name as the class which follows this format:

<?php
if ( !class_exists('FSO') )
    die('Framework SuperObject base class must be loaded to use its modules!');

class ClassName extends FSO_Module
{
    public function __construct()
    {
        parent::__construct();
        //  Stuff to do when the module is initialized
    }

    //  Module functions go here
}
?>

Module are not completely limited to this format - extending FSO_Module is merely a convenience to get access to all other modules with $this->mymodule syntax. If you don't need this, you can opt to not extend FSO_Module, and if you want remove the first two lines too.

Please see the relevant pages for details on how to use the various modules supplied with FSO.

Last modified April 13th, 2008 at 2:50 a.m.