TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-22-2009, 09:39 PM   #1 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default 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);
}
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 08-24-2009, 04:42 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design