zoukankan      html  css  js  c++  java
  • PHP类名获取的几种方式及单例模式实现

    参考:https://www.cnblogs.com/water0729/p/5803217.html

    <?php
    
    class foo {
        static public function test() {
            echo "foo.__CLASS__:".__CLASS__."
    ";
            echo "foo.get_class:".get_class()."
    ";
            echo "foo.get_called_class:".get_called_class()."
    ";
        }
    }
    
    class bar extends foo {
        
    }
    
    foo::test();
    echo "
    ";
    bar::test();
    ?>
    //结果
    foo.__CLASS__:foo
    foo.get_class:foo
    foo.get_called_class:foo
    
    foo.__CLASS__:foo
    foo.get_class:foo
    foo.get_called_class:bar
    获取类名的几种方式

    1.__CLASS__:获取当前的类名

    2.get_class():返回对象的类名

    3.get_called_class():后期静态绑定("Late Static Binding")类的名称,即静态方法调用者的类名

    <?php
    
    //通过get_called_class实现单例模式
    
    class Singleton{
       
       private static $instance; 
      
        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];
        }
    }
    
    ?>
  • 相关阅读:
    Uboot USB模式(RK3288变砖头的解决办法)
    C++ 解析一
    C++基础
    shell脚本分析二
    ASCII
    POJ 1970 The Game (DFS)
    PoJ1979 Red and Black (DFS)
    UVA 572 Oil Deposits (DFS)
    打印日历
    求第N个素数
  • 原文地址:https://www.cnblogs.com/yueyun00/p/10043505.html
Copyright © 2011-2022 走看看