I\'d like to iterate over an array and dynamically create functions based on each item. My pseudocode:
$array = array(\'one\', \'two\', \'three\');
foreach
class MethodTest
{
private $_methods = array();
public function __call($name, $arguments)
{
if (array_key_exists($name, $this->_methods)) {
$this->_methods[$name]($arguments);
}
else
{
$this->_methods[$name] = $arguments[0];
}
}
}
$obj = new MethodTest;
$array = array('one', 'two', 'three');
foreach ($array as $item)
{
// Dynamic creation
$obj->$item((function ($a){ echo "Test: ".$a[0]."\n"; }));
// Calling
$obj->$item($item);
}
The above example will output:
Test: one
Test: two
Test: three