<?php
class MyParent {
public static function tellSomething() {
return __CLASS__;
}
}
class MyChild extends MyParent {
}
echo MyChild::tellSomething();
The code above echos "MyParent". How can i get to name of child class - in this case "MyChild"? If it's possible...
I just simply need to know which child is calling the inherited method.
__CLASS__
is a pseudo-constant, that always refers to the class, where it is defined. With late-static-binding
the function get_called_class()
were introduced, that resolve the classname during runtime.
class MyParent {
public static function tellSomething() {
return get_called_class();
}
}
class MyChild extends MyParent {
}
echo MyChild::tellSomething();
(as a sidenote: usually methods don't need to know the class on were they are called)
What you are describing is called Late Static Bindings, and it was made available in PHP 5.3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With