Home  >  Article  >  Backend Development  >  PHP singleton mode entry example

PHP singleton mode entry example

WBOY
WBOYOriginal
2016-07-25 08:51:491173browse
  1. class mysql{
  2. privete static $instance ;//Save the instance
  3. //The constructor is declared as private to prevent direct creation of objects
  4. privete function __construct(){
  5. //Instantiation
  6. }
  7. //Single case method, determine whether it has been instantiated, and only instantiate it once
  8. public static function getInstance (){
  9. if(!isset( self::$instance )){
  10. self ::$instance = new self();
  11. }
  12. return self:: $instance;
  13. }
  14. //Prevent cloning objects
  15. private function __clone (){
  16. trigger_error ("not allow to clone.");
  17. }
  18. function test(){
  19. echo "test" ;
  20. }
  21. }
  22. $conn = mysql::getInstance ();
  23. $conn->test ();
  24. ?>
Copy code


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