Skip to content

Instantly share code, notes, and snippets.

@shuber
Created March 24, 2009 21:50
Show Gist options
  • Save shuber/84383 to your computer and use it in GitHub Desktop.
Save shuber/84383 to your computer and use it in GitHub Desktop.
phuby proc object pseudo-code
<?php
class Proc extends Object {
public $block;
public $parameters;
function initialize($block) {
$parameters = func_get_args();
$this->block = array_pop($parameters);
$this->parameters = $parameters;
}
function arity() {
if (empty($this->parameters)) {
return 0;
} else {
foreach ($this->parameters as $index => $parameter) {
if (is_array($parameter)) return (-1 * ($index + 1)) - 1;
}
return $index + 1;
}
}
function call($values = null) {
$values = func_get_args();
return $this->call_array($values);
}
function call_array($values = array()) {
$arguments = array();
foreach ($this->parameters as $index => $parameter) {
$parameter_name = (is_array($parameter)) ? $parameter[0] : $parameter;
if (isset($values[$index])) {
$arguments[$parameter_name] = $values[$index];
} elseif (is_array($parameter)) {
$arguments[$parameter_name] = $parameter[1];
} else {
trigger_error('Missing argument '.($index + 1).' in Proc::call()', E_USER_WARNING);
}
}
unset($index);
unset($parameter);
unset($parameter_name);
unset($values);
extract($arguments);
return eval($this->block);
}
}
function evaluate_block($block, $arguments = array()) {
$properties = array_keys($arguments);
$values = array_values($arguments);
return eval('return ('.build_function_call('new Proc', $properties, 'properties').')->call('.splat($values, 'values').');');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment