php __CLASS__、get_class()与get_called_class()的区别

获取类名的三种方法

__CLASS__获取当前的类名

get_class()与上面一样,都是获取当前的类名

get_called_class()获取当前主调类的类名

当涉及到继承时,在方法中使用类名。直接贴图了

 class A
      {
        function say(){
            echo "A is ".__CLASS__."<br/>";
            echo "A is ".get_class(). "<br/>";
            echo "A is ".get_called_class(). "<br/>";
        }
      }
      
      class B extends A
      {
           function say()
           {
              parent::say();
              echo "B is ".__CLASS__."<br/>";
              echo "B is ".get_class(). "<br/>";
              echo "B is ".get_called_class(). "<br/>";
           }
         
      }
      
      $b = new B();
      $b->say();
    

结果打印:

A is A
A is A
A is B
B is B
B is B
B is B

实用

MVC框架中,涉及到单例时很好用,一般在基类中:

public static function getInstance() {
        $class_name = get_called_class();
        if (isset(self::$instance[$class_name])) {
            return self::$instance[$class_name];
        }
        self::$instance[$class_name] = new $class_name;
        return self::$instance[$class_name];
    }

其他类只要继承这个类,然后通过 getInstance() 就实现了单例模式