F1e3ab214a976a39cfd713bc93deb10f

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?

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 !

Dbb32d6d1430a0d275aff3b16f20f8f4

Sofia

June 10, 2008, June 10, 2008 01:44, permalink

No rating. Login to rate!

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.

Your refactoring





Format Copy from initial code

or Cancel