Pav*_*rba 4 php oop parent-child
有没有办法如何在PHP中确定父类中的子实例?假设我们有这个代码:
class Parent {
public function InstanceOfChild() {
//What to put here to display "Child class is instance of ChildClass123"?
}
}
class ChildClass123 extends Parent {
//Some code here
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是(如果可能的话)是create方法InstanceOfChild(),它会告诉我子类的实例,因为很多类可以是我父类的子类,但我想(让我们说)log,哪个子调用哪个方法.感谢帮助!
有一个get_called_class()你正在寻找的功能.
class Parent1 {
public static function whoAmI() {
return get_called_class();
}
}
class Child1 extends Parent1 {}
print Child1::whoAmI(); // prints "Child1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2352 次 |
| 最近记录: |