Refactoring in PHP 5.2 land

Where I cope with missing late static bindings possibility and find my way of refactoring.

No late static bindings

Sadly late static bindings have been introduced in PHP 5.3 and what this means is that the easy refactoring of the two factories based on it cannot be done.
The basic idea here is to split the factories double nature of “makers” and “information sources” into two different interfaces keeping in line with the Interface Segregation Principle.
The first nature of the factories is represented by the WPSchedule_Interface_FactoryInterface

interface WPSchedule_Interface_FactoryInterface {

    public static function make( $hook, array $args = null );


whil the informant side of them is represented by the WPSchedule_Interface_FactoryDataInterface

interface WPSchedule_Interface_FactoryDataInterface {

     * @return string
    public function getOptionPostfix();

     * @return string
    public function getDefaultSlug();

     * @return string
    public function getDefaultClass();

     * @return array
    public function getSlugsAndClasses();

     * @param $slug
     * @return bool
    public function isLegitSlug( $slug );

Forwarding the calls

Once the two interfaces above are in place the following code can be summarized into the idea that calls to the make method are forwarded to a general purpose “option and slug” based factory while the query methods, now instance methods, are delegated to a common ancestor.
As an example here it is the actual WPSchedule_Time_Factory class and that’s meant as an entry point, together with the WPSchedule_Interval_Factory to the factory tree; it makes sense to follow the flow of code using it or watching it on GitHub.

class WPSchedule_Time_Factory extends WPSchedule_Abstract_FactoryData {

    public $optionPostfix   = '_schedule_time';
    public $defaultSlug     = 'now';
    public $defaultClass    = 'WPSchedule_Time_Now';
    public $slugsAndClasses = array(
        'now' => 'WPSchedule_Time_Now',
        '8pm' => 'WPSchedule_Time_EightPM',
        '8am' => 'WPSchedule_Time_EightAM'

    public static $type = 'time';

    public static function make( $hook, array $args = null ) {

        return WPSchedule_Factory_OptionFactory::make( self::$type, $hook, $args );


WPSchedule_Factory_OptionFactory::register( WPSchedule_Time_Factory::$type, new WPSchedule_Time_Factory() ); 

Autoloading and slick tricks

The last line in the file defining the class above leverages auto-loading, and hence whole file reading, to register the class and its information with the general purpose factory.
That’s not as elegant as it can get but still it’s effective.


More TDD on the `WPSchedule_Schedule_Schedule’ class to close the project.