<?PHPclassa{function__construct () {Echo __class__; } Static functionname () {Echo __class__; }}classBextendsa{}$objB=NewB ();//Output AB::name ();//Output A
At this point, whether you instantiate B or call the static method directly, Echo will come out with a.
And what I actually want to get is the name of sub-category B! How does that work?
PHP comes with two functions get_class () and Get_called_class () to solve this problem.
Get_class () is used for instance invocation, the addition of parameters ($this) solves the problem of subclass inheritance calls, and Get_called_class () is used for static method calls.
<?PHPclassa{function__construct () {Echo Get_class($this); } Static functionname () {EchoGet_called_class (); }}classBextendsa{}$objB=NewB ();//Output BB::name ();//Output B
How does the inheritance method of PHP get the subclass name? Get_class () and Get_called_class