如何获取调用类的名称(在PHP中)

前端之家收集整理的这篇文章主要介绍了如何获取调用类的名称(在PHP中)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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();
原文链接:https://www.f2er.com/php/138650.html

猜你在找的PHP相关文章