Quello che ho scritto mi fa un po' vergognare, ma non saprei in che altro modo si possa implementare quello che chiedi:
Codice PHP:<pre>
<?php
class MyClass {
var $_methods = array();
function createMethod($name, $args, $code){
$this->_methods[$name]['code'] = $code;
$this->_methods[$name]['args'] = $args;
}
function __call($name, $args) {
if (!method_exists($this, $name) && isset($this->_methods[$name])) {
if (count($args) == count($this->_methods[$name]['args'])) {
$args = array_combine($this->_methods[$name]['args'], $args);
return $this->_call_method($this->_methods[$name]['code'], $args);
}
}
}
function _call_method($___code, $___arguments) {
extract($___arguments);
return eval($___code);
}
}
$obj = new MyClass();
$obj->createMethod('my_function', array('arg1', 'arg2'), 'print("{$arg1}\r\n"); var_dump($arg2);');
$obj->my_function('arg 1', 'arg 2');
?>
</pre>

Rispondi quotando