views:

31

answers:

2
define('anActionType', 1);
$actionTypes = array(anActionType => 'anActionType');
class core {
    public $callbacks = array();
    public $plugins = array();
    public function __construct() {
        $this->plugins[] = new admin();
        $this->plugins[] = new client();
    }
}
abstract class plugin {
    public function registerCallback($callbackMethod, $onAction) {
        if (!isset($this->callbacks[$onAction]))
            $this->callbacks[$onAction] = array();

        global $actionTypes;
        echo "Calling $callbackMethod in $callbacksClass because we got {$actionTypes[$onAction]}" . PHP_EOL;

        // How do I get $callbacksClass?

        $this->callbacks[$onAction][] = $callbackMethod;
    }
}
class admin extends plugin {
    public function __construct() {
        $this->registerCallback('onTiny', anActionType);
    }
    public function onTiny() { echo 'tinyAdmin'; }
}
class client extends plugin {
    public function __construct() {
        $this->registerCallback('onTiny', anActionType);
    }
    public function onTiny() { echo 'tinyClient'; }
}
$o = new core();

$callbacksClass should be admin or client. Or am I missing the point here completely and should go about this another way? It should be noted that I will only accept an answer that does not require me to send the classname as an argument to the registerCallback method.

+2  A: 

use get_class():

$this->callbacks[$onAction][] = $callbackMethod;
$className = get_class($this);

// Call callback method
$className->$callbackMethod();
hopeseekr
Alright! Another win!
hopeseekr
@hopeseekr, It's exactly what I was looking for. :)
Mark Tomlin
+1  A: 

You should really do something like:

$this->registerCallback(array($this, 'onTiny'), anActionType);

That is how PHP works with handles to object methods.

konforce
That's how the PHP internal callback functions (like preg_replace_callback) work, but not his class. His class would break if he used that syntax.
hopeseekr
He should change his class to work with normal PHP callbacks, unless there's a very good reason not to.
konforce
@konforce, there is a very good reason not too. But thanks for the reply.
Mark Tomlin