square(3); class ImportCacheError extends Exception {} class ImportFileError extends Exception {} class ImportAttributeError extends Exception {} function import($file) { static $imported = array(); if (!isset($imported[$file])) { try { $imported[$file] = new Import($file); } catch (ImportFileError $e) { return false; } } return $imported[$file]; } class Import { static $cache_dir; public $__variables__, $__functions__, $__classes__, $__file__, $__cache_file__, $__cached__, $__cached_filemtime__, $__filemtime__; function __construct($file) { $this->__file__ = $file; if (!is_file($file)) { throw new ImportFileError('File "'.$file.'" not found for import.'); } if (!is_writeable(Import::$cache_dir)) { throw new ImportCacheError('Cannot write to "'.Import::$cache_dir.'"'); } $this->__filemtime__ = filemtime($file); $this->__cache_file__ = Import::$cache_dir.md5($file).'.php'; $this->__cached__ = is_file($this->__cache_file__) ? true : false; if ($this->__cached__) { $this->__loadCache__(); if ($this->__cached_filemtime__ != $this->__filemtime__) { $this->__reloadImport__(); $this->__saveCache__(); } else { $this->__loadImport__(); } } else { $this->__reloadImport__(); $this->__saveCache__(); } } public function __call($attr, $args) { $attr = strtolower($attr); if (in_array($attr, $this->__functions__)) { return call_user_func_array($attr, $args); } elseif (in_array($attr, $this->__classes__)) { return $attr; } else { throw new ImportAttributeError('Import has no "'.$attr.'" attribute.'); } } public function __get($attr) { if (isset($this->__variables__[$attr])) { return $this->__variables__[$attr]; } elseif (in_array(strtolower($attr), $this->__classes__)) { return $attr; } else { throw new ImportAttributeError('Import has no "'.$attr.'" attribute.'); } } private function __reloadImport__() { $this->pre_vars = get_defined_vars(); $this->pre_funcs = get_defined_functions(); $this->pre_funcs = $this->pre_funcs['user']; $this->pre_classes = get_declared_classes(); include $this->__file__; $this->__variables__ = array_slice(get_defined_vars(), count($this->pre_vars)); $this->post_funcs = get_defined_functions(); $this->post_funcs = $this->post_funcs['user']; $this->__functions__ = array_slice($this->post_funcs, count($this->pre_funcs)); $this->__classes__ = array_slice(get_declared_classes(), count($this->pre_classes)); unset($this->pre_vars, $this->pre_funcs, $this->pre_classes, $this->post_funcs); } private function __loadCache__() { $cache = include $this->__cache_file__; $this->__classes__ = $cache['classes']; $this->__functions__ = $cache['functions']; $this->__cached_filemtime__ = $cache['filemtime']; } private function __loadImport__() { $this->pre_vars = get_defined_vars(); include $this->__file__; $this->__variables__ = array_slice(get_defined_vars(), count($this->pre_vars) - 1); unset($this->pre_vars); } private function __saveCache__() { $cache = ' '. var_export($this->__functions__, true).','. '\'classes\' => '.var_export($this->__classes__, true).', \'filemtime\' => '.filemtime($this->__file__).'); ?>'; file_put_contents($this->__cache_file__, $cache); } } ?>