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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//==========================================================================
//=== THE POWER HORSE!
//==========================================================================
 
abstract class dynamic_params {
       
       //the only method needed
       final public function call($method, array $set_params) {
              
              //get what paramters the method expects
              $reflected_method = new ReflectionMethod(get_class($this), $method);
              
              //get ready to build up the list of params
              $params = array();
              
              //now loop through the properties of the method that is about to be called
              foreach ($reflected_method->getParameters() as $i => $param) {
                     
                     //see if it exists
                     if (isset($set_params[$param->getName()])) {
                            $params[] = "'{$set_params[$param->getName()]}'";
                            } else {
                            if ($param->isOptional()) {
                                   $params[] = 'null';
                                   } else {
                                   trigger_error("The parameter \"{$param->getName()}\" is required!", E_USER_ERROR);
                            }
                     }
                     
              }
              
              //build up the params ready to be evaled in the method
              $params = implode(',',$params);
              
              //execute the constructed method
              eval("\$this->{$method}({$params});");
              
       }
       
}
 
//==========================================================================
//=== EXAMPLE
//==========================================================================
 
class typewriter extends dynamic_params {
       
       public function write($string, $color = 'black', $size = '12') {
              print "<span style='font-size:{$size}px;color:{$color};'>{$string}</span>";
       }
       
}
 
 
$machine = new typewriter();
 
//calls the write() method with string and size params set, but NOT the middle color param ;-)
$machine->call(
       //the method to call in the class
       'write', 
       //the paramters that you want to pass to the method (all left out paramters are assigned with null
       array(
              'string' => 'hello world', 
              'size' => '16'
              )
       );
 
//calls the write() method with just a size set, resulting in a fatal error because string is required
$machine->call(
       //the method to call in the class
       'write', 
       //the paramters that you want to pass to the method (all left out paramters are assigned with null
       array(
              'size' => '16'
              )
       );