All pastes #746242 Raw Edit

Miscellany

public php v1 · immutable
·published 2007-10-23 02:21 UTC
rendered paste body
<?phpclass ImportItemError extends Exception {}class ImportFileError extends Exception {}function import($file) {	static $imported = array();	$file = realpath($file);	if (isset($imported[$file])) {		return $imported[$file];	}	try {		$imported[$file] = new Import($file);		return $imported[$file];	}	catch (ImportFileError $e) {		return false;	}}class Import {	private	$_variables,	$_constants,	$_functions,	$_classes;	public function __construct($file) {		if (!is_file($file)) {			throw new ImportFileError('Unable to find "'.$file.'" for import.');		}		$this->pre_vars = get_defined_vars();		$this->pre_consts = get_defined_constants();		$this->pre_funcs = get_defined_functions();		$this->pre_funcs = $this->pre_funcs['user'];		$this->pre_classes = get_declared_classes();		include $file;		$post_vars = get_defined_vars();		$post_consts = get_defined_constants();		$post_funcs = get_defined_functions();		$post_funcs = $post_funcs['user'];		$post_classes = get_declared_classes();		$this->_variables = array_diff($post_vars, $this->pre_vars);		$this->_constants = array_diff($post_consts, $this->pre_consts);		$this->_functions = array_diff($post_funcs, $this->pre_funcs);		$this->_classes = array_diff($post_classes, $this->pre_classes);		unset($this->pre_vars, $this->pre_consts, $this->pre_funcs, $this->pre_classes);	}	public function allVariables() {		return $this->_variables;	}	public function allConstants() {		return $this->_constants;	}	public function allFunctions() {		return $this->_functions;	}	public function allClasses() {		return $this->_classes;	}	public function __get($item) {		if ($key = array_search($item, $this->_classes)) {			return $this->_classes[$key];		}		elseif (isset($this->_variables[$item])) {			$this->_variables[$item];		}		else {			throw new ImportItemError('Imported variable or class not found.');		}	}	public function __call($item, $args = array()) {		if ($key = array_search($item, $this->_classes)) {			return $this->_classes[$key];		}		elseif (in_array($item, $this->_functions)) {			call_user_func_array($item, $args);		}		else {			throw new ImportItemError('Imported function or class not found.');		}	}}?>