Created
June 29, 2011 01:12
-
-
Save durango/1052676 to your computer and use it in GitHub Desktop.
Just showing someone the basics of closures...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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