snapInMethods PHP class: add methods to parent classes from child classes

I just had to create a singleton which would allow child classes to add methods to the parent instance.

While the end result turned out to be pretty simple use of user_func_array, the process was repetitive enough for each group of extendable methods that I ended up standardizing it in a separate class, and it’s a pretty handy thing to have.

To use it, include the class file and instantiate in the parent class as follows:

$this->_extendableMethods = new snapInMethods();

Access from the child class with

$this->_parent = parent::getInstance();

$this->_parent->_extendableMethods->add(‘handle’, ‘displayName’, __CLASS__, ‘callback’);  //creates a new method from the child class

$this->_parent->_extendableMethods->get(); //returns an associative array of added methods as Array( handle => Array(‘displayName’, ‘class’, ‘callback’))

$this->_parent->_extendableMethods->del(‘handle’); //removes specified method from extended methods

$this->_extendableMethods->exec(‘handle’, array($param1, $param2, $etc)); //runs the specified extended method, passing each array element as an argument. The purpose of the class is that you can run this from the parent class, and it will be aware of any methods that the children have added.

Here’s the class file:

<?php
/*
Class: snapInMethods
Description: Allows you to easily define a type of method which other classes can add methods to on a parent class. Best suited for a singleton where new classes need to be able to extend functionality.
Usage:

include_once('class.snapInMethods.php');
class parentClass
{

    public $_snapInMethods;

    method __construct()
    {
        $this->_snapInMethods = new snapInMethods();
    }

    method runSnapInMethods()
    {
        $methods = $this->_snapInMethods->get();
        foreach ($methods as $methodhandle => $data ) $this->_snapInMethods($methodhandle, array($param1, $param2, $etc ));
    }
}

class childMethod extends parentClass
{
    method __construct()
    {
        parent::_snapInMethods->add('handle', 'displayName', __CLASS__, 'callback');
    }

    method callback($parameters)
    {
        //process data sent when method is called by parent class.
    }
 }

 *************************************************************************************/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

class snapInMethods
{
    protected $_methods = array();

    public function add($handle, $displayName, $className, $callback)
    {
        $this->_methods[$handle] = array('display' => $displayName, 'class' => $className, 'callback' => $callback);
    }

    public function get()
    {
        return $this->_methods;
    }

    public function del($handle)
    {
        if (isset($this->_methods[$handle])) unset($this->_methods[$handle]);
    }

    public function exec($handle, array $params)
    {
        return call_user_func_array(array($this->_methods[$handle]['classname'], $this->_methods[$handle]['callback']), $params);
    }
}
Enhanced by Zemanta

Tags: , , , , , , , , , , , , , , , , , , ,