Created
August 7, 2011 18:55
-
-
Save drslump/1130650 to your computer and use it in GitHub Desktop.
Drupal Class to global functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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