【问题标题】:How to get the name of the calling class (in PHP)如何获取调用类的名称(在 PHP 中)
【发布时间】:2011-04-06 23:05:15
【问题描述】:
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 方法的答案。

【问题讨论】:

  • Erm,这两种方法都是实例方法(不是静态方法),所以如果你真的需要类名用于其他目的然后只是回显它(即调用回调),你必须提供一个实例而不是一个类名可能......

标签: php class abstraction abstract


【解决方案1】:

从 PHP 8+ 开始,您可以使用 static::class 而不是 get_class($this)

这也是通过 PHP 代码嗅探器和规则 SlevomatCodingStandard.Classes.ModernClassNameReference 自动修复的

【讨论】:

  • 啊,非常感谢您的更新!
【解决方案2】:

使用get_class():

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

// Call callback method
$className->$callbackMethod();

【讨论】:

  • 这正是我想要的。 :)
  • this 不返回调用类,只返回 $this 的类。如果你用另一个类调用 registerCallback ,它会失败。使 registerCallback 受保护或使用 matthews 方法。
  • 您也可以使用static::class 代替get_class($this)。如果我没记错的话,只能在 PHP 8+ 中使用
【解决方案3】:

如果有人像我一样来这里寻找如何从另一个类中获取调用类的名称,请查看https://gist.github.com/1122679

编辑:粘贴代码

function get_calling_class() {

    //get the trace
    $trace = debug_backtrace();

    // Get the class that is asking for who awoke it
    $class = $trace[1]['class'];

    // +1 to i cos we have to account for calling this function
    for ( $i=1; $i<count( $trace ); $i++ ) {
        if ( isset( $trace[$i] ) ) // is it set?
             if ( $class != $trace[$i]['class'] ) // is it a different class
                 return $trace[$i]['class'];
    }
}

EG

class A {
    function t() {
        echo get_calling_class();
    }
}

class B {
    function x() {
        $a = new A;
        $a->t();
    }
}

$b = new B;
$b->x(); // prints B

【讨论】:

  • 我认为,如果您将代码放在这里,您将获得更多的赞成票,而不是我的。 (虽然,我确实喜欢 GitHub,只是如果它的源代码托管在这里会更好。)
  • 为此干杯。帮了我一把。
【解决方案4】:

你真的应该这样做:

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

这就是 PHP 如何处理对象方法的句柄。

【讨论】:

  • PHP 内部回调函数(如 preg_replace_callback)是这样工作的,但他的类不是这样。如果他使用这种语法,他的课就会中断。
  • 他应该改变他的类以使用正常的 PHP 回调,除非有很好的理由不这样做。
猜你喜欢
  • 2010-12-14
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 2011-02-20
  • 2013-01-29
  • 1970-01-01
相关资源
最近更新 更多