Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Autoloading?
#1
I'd been thinking to write a tutorial recently (which would go on the wiki) on how to build a relatively simple GS blog plugin from scratch. This would utilise OOP techniques to accomplish it elegantly. Currently, GetSimple statically uses the include/require functions in the plugin files, but this leads to problems when there are multiple plugin dependencies (i.e. if the plugins are loaded in the wrong order, the classes will not exist yet in the runtime).

To remedy this, is it possible that a plugin hook be created for autoloading classes? It would be a hook that takes a function (or object method) name and add it to the autoload stack. This ensures that regardless of the order in which the plugins are loaded, when a class is needed, it will be loaded if it hasn't yet.

All the hook needs to do (in the pre-header or pre-template) is queue all of the functions called to by that hook, then loop through the queue, using spl_autoload_register to add that function to the autoload stack.

Example:
PHP Code:
// Code for plugin developers (in their plugin_name.php file
// 1. In your plugin, use the autoload hook
add_hook'autoload''my_autoloader' ); // adds function 'my_autoloader' to queue/array called $autoloaders

// 2. define your autoloader (for example, this one assumes a file structure '/plugins/plugin_name/Classname.class.php')
function my_autoloader($class) {
  
$file 'plugin_name/'$file 'class.php'
  
if (file_exists($file)) include($file);


PHP Code:
// algorithm for how the hook works (to be integrated into core)
foreach ($autoloaders as $function) {
  
spl_autoload_register($function)

Reply


Messages In This Thread
Class Autoloading? - by Angryboy - 2014-03-14, 04:40:58
RE: Class Autoloading? - by shawn_a - 2014-03-14, 05:02:02
RE: Class Autoloading? - by Angryboy - 2014-03-14, 05:07:13
RE: Class Autoloading? - by shawn_a - 2014-03-14, 06:28:05
RE: Class Autoloading? - by Angryboy - 2014-03-14, 08:43:46
RE: Class Autoloading? - by shawn_a - 2014-03-14, 09:44:52
RE: Class Autoloading? - by Angryboy - 2014-03-14, 10:15:01
RE: Class Autoloading? - by shawn_a - 2014-03-14, 10:27:05
RE: Class Autoloading? - by shawn_a - 2014-03-14, 10:34:37
RE: Class Autoloading? - by shawn_a - 2014-03-14, 12:34:53
RE: Class Autoloading? - by Angryboy - 2014-03-14, 17:59:05



Users browsing this thread: 1 Guest(s)