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
應該是管理員或客戶端。或者我是否完全忽略了這一點,應該以另一種方式進行探討?應該指出,我將只接受一個答案,它不要求我將類名作爲參數發送到registerCallback方法。如何獲取呼叫類的名稱(以PHP爲單位)
Erm,這兩種方法都是實例方法(不是靜態的),所以如果你真的需要用於另一個目的的類名然後只是回顯它(即調用回調),你必須提供一個實例,而不是一個類名可能... – Wrikken 2010-09-01 21:14:23