Skip to content

Instantly share code, notes, and snippets.

@drslump
Created August 7, 2011 18:55
Show Gist options
  • Save drslump/1130650 to your computer and use it in GitHub Desktop.
Save drslump/1130650 to your computer and use it in GitHub Desktop.
Drupal Class to global functions
<?php
class DrupalClass {
static $instances = array();
public function __construct()
{
$class = get_class($this);
if (isset(self::$instances[$class])) return;
// Generate global functions from the class methods
$methods = get_class_methods($this);
foreach ($methods as $method) {
eval("function {$class}_{$method}(){
return call_user_func_array(
array(DrupalClass::\$instances['$class'], '$method'),
$args = func_get_args()
);
}");
}
// Register this instance in the registry
self::$instances[$class] = $this;
}
}
<?php
class MyModule extends DrupalClass {
public function hook_menu(...)
{
...
}
public function hook_save(...)
{
...
}
}
// Instantiate to register global functions
new MyModule();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment