08-22-2009, 09:39 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
So..I was bored...and was working with jQuery
When I thought why not make a jQuery-esk look a like now that we have the __invoke method. Libraries by default can be in the /Libraries directory relative to the Worker file. Or you can specify the library directory.
Note: There really isn't any legitimate use for this btw. It's just for fun.
Use:
php Code:
_('database')->query('select * from foo'); $_('database')->query('select * from foo');
Class:
php Code:
<?phpclass Worker { private $library; private $libraryDirectory; private static $instanceOfSelf; public static function getInstance () { if( ! $instanceOfSelf instanceof Worker ) self:: $instanceOfSelf = new Worker; return self:: $instanceOfSelf; } public function __invoke( $lib ) { if( ! array_key_exists($lib, $this-> library) ) $this->__loadLibrary ($lib); return $this-> library[ strtolower($lib) ]; } private function __construct($dir = null) { $this-> library = array(); $this-> libraryDirectory = ! is_dir($dir) ? $dir : dirname(__FILE__) . '/Libraries/'; } private function __loadLibrary ($library) { $libraryToLoad = $this-> libraryDirectory . '/Lib_' . $library . '.php'; if( ! file_exists($libraryToLoad) ) throw new Exception ('Library does not exists'); require_once $libraryToLoad; $this-> library[strtolower ($library)] = new {'Lib_' . $library}; } }$_ = Worker:: getInstance(); function _( $func ){ global $_; return $_($func); }
Alternatively if you don't want the $_ global..
php Code:
function _($func){ static $worker; if( ! $worker instanceof Worker ) $worker = Worker:: getInstance(); return $worker($func); }
|
|
|
|