TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   So..I was bored...and was working with jQuery (http://www.talkphp.com/general/4889-so-i-bored-working-jquery.html)

Enfernikus 08-22-2009 09:39 PM

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:
<?php

class 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);
}

tony 08-24-2009 04:42 PM

This looks good, although I wouldn't think you would need a class for this, a function like $_ or _ would be enough. However, it is appealing to code some php in the jquery form :P. cool.


All times are GMT. The time now is 02:01 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0