php simple singleton

BRIEF DESCRIPTION php singleton

  

 

<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2019/9/5
 * Time: 20:58
 */
/*
  Example 1. Single mode can be instantiated by itself
  2. Save object instance has a static member variables
  3. The access to this example has a public static method (commonly getInstance () method instantiates singleton)
  4. Whether instanceof operator can be detected by the class already instantiated
  */
class single
{
    /**
     * Private constructor
     * single constructor.
     */
    private function __construct()
    {

    }

    /**
     * Private constructor preventing clone
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    / * Static method storage instance * / 
    Private  static  $ interion ;

    /**
     * To get through this example
     * @return single
     */
    public function getInstance(){
        if (empty($interion)){
            self::$interion=new single();
        }
        return self::$interion;
    }

}

 

  Example 1. Single mode can be instantiated by itself
  2. Save object instance has a static member variables
  3. The access to this example has a public static method (commonly getInstance () method instantiates singleton)
  4. instanceof operator can be detected by whether the class is already instantiated 

advantage singletons:
 a php process only a single instance of an embodiment, to reduce memory usage is reduced resource consumption. For example, the number of your adjusted php process, as the number of processes need to be aware php
mysql count the number of connections. Because php mysql using the Singleton pattern, your PHP process number is the number of connections mysql. (This time also need to pay attention to your mysql connections must be greater than
the number you process php, mysql otherwise wait for the release of the connection that caused a domino-like waiting for causing a system crash)
shortcomings Singleton pattern:
no abstraction layer is difficult to extend unsuitable variability strong examples


Guess you like

Origin www.cnblogs.com/sgj123/p/11470097.html