1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
static public function invoke($hook) {
$packages = array();
$results = array();
$packages['libraries'] = self::$libraries;
$packages['modules'] = self::$modules;
foreach((array) $packages AS $type => $names){
foreach((array) $names AS $name){
switch($type){
case 'libraries':
$result = @eval("{$name}::{$hook}();");
break;
case 'modules':
$result = @eval("{$name}Module::{$hook}();");
break;
}
if (is_array($results)){
$results = array_merge($results, (array) $result);
}
}
}
return $results;
}
Refactorings
No refactoring yet !
Sofia
June 10, 2008, June 10, 2008 01:44, permalink
I don't see the need to use eval. If the class is in the scope why not just call it? Anyway the reflection class might be useful as might the is_callable function (http://pt2.php.net/manual/en/function.is-callable.php). Check them out.
Hey guys, I should have really thought ahead with this before I went ahead and wrote a good 15 classes but either way my core class needs a mechanism to invoke 'hooks' within each of the other classes available within the scope. Eval() does work, however it obviously outputs an error when the 'hook' does not exist which is certainly no good.. Is there any good clean way this can be done?