Home  >  Article  >  Backend Development  >  think about php late binding issues

think about php late binding issues

WBOY
WBOYOriginal
2016-07-06 13:54:071515browse

Similar to the following code

<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>

Then use the url or configuration to have a subclass to inherit father and call the instance() method to instantiate
How to determine which subclass is called statically?

Reply content:

Similar to the following code

<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>

Then use the url or configuration to have a subclass to inherit father and call the instance() method to instantiate
How to determine which subclass is called statically?

In the method, you can use the get_called_class() function to get the class name that calls this method, which is designed for late static binding.

namespaceNamespace

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn