【发布时间】:2015-03-08 09:40:45
【问题描述】:
如何在非静态上下文中从父作用域获取子类方法名称?
使用__METHOD__ 魔术常数获取当前方法名称很简单,但我正在尝试获取发起请求的方法。
get_class($this) 在超类范围内返回调用者类。获取方法需要类似的实现。
例如:
class A {
public function __construct() {
// Here I want know which method initiated the call.
// In current scenario its the method foo() of the B class.
}
}
class B extends A {
public function foo() {
}
}
$b = new B();
$b->foo();
【问题讨论】:
-
B::foo 和父类的构造函数有什么关系?
-
您能澄清一下您要做什么吗?你在 A 的构造函数中添加的注释似乎表明你要输出 B::foo,但是构造函数是在 foo 之前调用的。
-
@MightyPork 它与构造函数无关。
-
@mtinsley 添加了更多细节。请检查
标签: php class oop inheritance methods