Home > Backend Development > PHP Tutorial > think about php late binding issues

think about php late binding issues

WBOY
Release: 2016-07-06 13:54:07
Original
1579 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template