Heim  >  Artikel  >  Backend-Entwicklung  >  think关于php后期绑定问题

think关于php后期绑定问题

WBOY
WBOYOriginal
2016-07-06 13:54:071511Durchsuche

类似与如下代码

<code>class father(){
 public function __construct(){
    echo "father";
 }
  
   public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }
}
</code>

然后通过url 或者配置 有子类来继承father 调用 instance()方法来实列化
请问如何确定 static调用的那个子类.

回复内容:

类似与如下代码

<code>class father(){
 public function __construct(){
    echo "father";
 }
  
   public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }
}
</code>

然后通过url 或者配置 有子类来继承father 调用 instance()方法来实列化
请问如何确定 static调用的那个子类.

在方法中可以通过get_called_class()这个函数来获得调用此方法的类名,针对后期静态绑定而设计。

namespace命名空间

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn