PHP Interview Questions(8)

面试时间:2018 年 9 月 21 日,星期五,下午三点。


单例模式

final class Singleton
{
    /**
     * @var Singleton
     */
    private static $instance;
    /**
     * gets the instance via lazy initialization (created on first usage)
     */
    public static function getInstance(): Singleton
    {
        if (null === static::$instance) {
            static::$instance = new static();
        }
        return static::$instance;
    }
    /**
     * is not allowed to call from outside to prevent from creating multiple instances,
     * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead
     */
    private function __construct()
    {
    }
    /**
     * prevent the instance from being cloned (which would create a second instance of it)
     */
    private function __clone()
    {
    }
    /**
     * prevent from being unserialized (which would create a second instance of it)
     */
    private function __wakeup()
    {
    }
}

https://github.com/domnikl/DesignPatternsPHP/blob/master/Creational/Singleton/Singleton.php

https://phptherightway.com/pages/Design-Patterns.html

https://stackoverflow.com/questions/203336/creating-the-singleton-design-pattern-in-php5

MySQL 分区表

https://www.jianshu.com/p/89311703b320

363 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *