PHP Get name of child class in static function [du

2019-03-06 14:21发布

Is something like this possible?

<?
class A
{
   public static function fun()
   {
      var_dump(get_class(child)); //bool(false) //should return B
   }
}
class B extends A
{
   public static function fun()
   {
      parent::fun();
   }
}

B::fun();
?>

标签: php class
2条回答
姐就是有狂的资本
2楼-- · 2019-03-06 14:44
<?php
class B extends A
{
   public static function fun()
   {
      parent::fun();
   }
}
class A
{
   public static function fun()
   {
      var_dump(get_called_class());
   }
}

B::fun();

http://php.net/manual/en/function.get-called-class.php

查看更多
神经病院院长
3楼-- · 2019-03-06 14:52

As of PHP 5.3, there is get_called_class() for this purpose:

echo get_called_class(); // yields "B"
查看更多
登录 后发表回答