Yet Another EventManager Class in PHP (

Some months ago I published an article about How to add events to a PHP project (1st EventManager). It should have been the basis for a later article about how to use events to index a site, still to be written.

The 1st EventManager class was ready to use, but some days ago I discovered the Event Dispatcher component of symfony (sfEventDispatcher class), whose Recipes page shows some usage examples. It proved that my EventManager class needed some reworking to have comparable power. Continue reading “Yet Another EventManager Class in PHP (“

Yet Another Singleton Class in PHP (

class SingletonException extends Exception
{}

abstract class Singleton {
    
    /**
     * Is the repository for instances of subclasses
     *
     * @var array
     */
    static private $_instances = array();
    
    /**
     * 1: Disables the 'new' operation
     * 2: Calls Singleton::_construct() with all the arguments passed to
     * Singleton::instance(), except the first one, which is the class name
     * 3: Creates a shortcut for accessing the instance by the class name
     *
     * @param array $args
     */
    private function __construct( $args )
    {
        $init = array($this, '_construct');
        call_user_func_array( $init, $args );
        
        $class = get_class($this);
        $shortcut = "
if (! function_exists('$class'))
{
    function $class()
    {
        return Singleton::instance('$class');
    }
}
";
        eval($shortcut);
    }
    
    /**
     * Disables the 'clone' operation
     */
    private function __clone()
    {}
    
    /**
     * 1: Initializes the instance
     * 2: Gets called once and automatically by Singleton::instance()
     * 3: Receives all the arguments passed to Singleton::instance(), except the
     * first one, which is the class name
     */
    protected function _construct()
    {}
    
    /**
     * Returns true if there is an instance of the $class in the repository
     *
     * @param string $class
     * @return boolean
     */
    static public function instanceAvailable( $class )
    {
        return isset( self::$_instances[ $class ] );
    }
    
    /**
     * 1: Returns the instance of the $class
     * 2: Creates, initializes and checks the instance the first time it gets
     * called with a given $class name
     *
     * @param string $class
     * @return Singleton
     */
    static public function instance( $class )
    {
        if (! self::instanceAvailable( $class ))
        {
            $args = array_slice( func_get_args(), 1 );
            $instance = new $class( $args );
            if ($instance instanceof self)
            {
                self::$_instances[ $class ] = $instance;
            }
            else
            {
                throw new SingletonException('Expected a Singleton subclass');
            }
        }
        return self::$_instances[ $class ];
    }
}
Continue reading “Yet Another Singleton Class in PHP (“

jQuery Window Name Transport Plugin

Yesterday I’ve released a jQuery window name transport plugin: jquery-wnt.js

Inside it’s much simpler than other attempts.

Outside it exposes two methods:

  • $.wnt.get( url, data, callback )
  • $.wnt.post( url, data, callback )

The arguments are:

  • url: the url you want to fetch
  • data: the data you want to send, as a js object
  • callback: the function that will take care of the response, received as the only argument

It works in IE8, FF3.5, Safari 4, Chrome 3, and Opera 10.

Known issues:

  • the query string of the url of the get is not honored, so you should pass those parameters as data
    FIXED 2009-10-07

WNT pages are here and here.