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应该是admin或者client.或者我完全忘记了这一点,应该这样呢?应该注意的是,我只接受一个不需要我将类名作为参数发送到registerCallback方法的答案.
使用
get_class()
:
$this->callbacks[$onAction][] = $callbackMethod; $className = get_class($this); // Call callback method $className->$callbackMethod();