PHP singleton mode entry example

WBOY
Release: 2016-07-25 08:51:49
Original
1172 people have browsed it
  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


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