Skip to content

Instantly share code, notes, and snippets.

@durango
Created June 29, 2011 01:12
Show Gist options
  • Save durango/1052676 to your computer and use it in GitHub Desktop.
Save durango/1052676 to your computer and use it in GitHub Desktop.
Just showing someone the basics of closures...
<?php
class item {
public $player_id, $ids, $sql, $vars = array();
private $functions = array();
function __construct($sql=null) {
$this->sql = $sql;
$this->sql->query('SHOW FIELDS FROM items');
for($x=0;$x<$this->sql->rows;$x++) {
$this->sql->fetch($x);
$data = $this->sql->data;
$name = 'find_by_'.strtolower(str_replace(' ','_',$data[0]));
$this->$name = function($id) {
echo 'hello';
};
}
}
function _methods() {
if(count($this->functions) < 1) return false;
foreach($this->functions AS $func => $function)
$funct[] = $func;
return implode(', ', $funct);
}
function __set($name, $data) {
if(is_callable($data)) $this->functions[$name] = $data;
else $this->vars[$name] = $data;
}
function __call($method, $args) {
if(isset($this->functions[$method]))
call_user_func_array($this->functions[$method], $args);
else
throw new Exception('Invalid call method: '.$method.' in class '.__CLASS__);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment