Home  >  Article  >  Backend Development  >  PHP design pattern singleton pattern learning

PHP design pattern singleton pattern learning

WBOY
WBOYOriginal
2016-07-25 09:07:44825browse
  1. class A{
  2. private function __construct(){}
  3. }
  4. $a = new A();
  5. ?>
Copy the code

The program running result is: Fatal error: Call to private A::__construct() from invalid context in E:PHPProjectstest.php on line 6

We have prohibited the external use of new to instantiate this class. How do we allow users to access this class? The front door is blocked, we need to leave a back door for users. The solution is: static modified method, you can directly access this method without instantiating a class. //Classes that cannot be instantiated with new.
//static methods are reserved for external access.
//Return the instance inside the method.

  1. class A{
  2. private function __construct(){}
  3. static function getClassA(){
  4. $a = new A();
  5. return $a;
  6. }
  7. }
  8. // Look What is indeed returned here is an instance of A. But it is not the same object.
  9. $a1 = A::getClassA();
  10. $a2 = A::getClassA();
  11. echo "The class of $a1 is".get_class( $a1)." , $a2 is ".get_class($a1);
  12. if($a1 === $a2){
  13. echo "
    $a1 $a2 points to the same object.";
  14. }else{
  15. echo "
    $a1 $a2 is not an object.";
  16. }
  17. ?>
Copy the code

The program running result is: The class of $a1 is A , $a2 is A $a1 $a2 is not an object.

We have returned the instance of A through the static method. But there's still a problem. How do we ensure that we obtain the same instance through multiple operations?

Solution: There is only one static attribute internally. Static attributes can be effectively called by static methods. Also set this property to private to prevent external calls. First set this property to null. Before each return of the object, first determine whether this property is null. If null, create a new instance of this class and assign it to this static property. If it is not empty, return the static property pointing to the instance. //Classes that cannot be instantiated with new.
//static methods are reserved for external access.
//Return the instance inside the method.
//Define static attributes to ensure that this instance can be called by static methods.
//Add judgment part.

  1. class A{
  2. private static $a = null;
  3. private function __construct(){}
  4. static function getClassA(){
  5. if( null == self::$a){
  6. self::$a = new A();
  7. }
  8. return self::$a;
  9. }
  10. }
  11. // You can see that the instance of A is indeed returned here. But it is not the same object.
  12. $a1 = A ::getClassA();
  13. $a2 = A::getClassA();
  14. echo "The class of $a1 is ".get_class($a1)." , $a2 is ".get_class($a1);
  15. if($ a1 === $a2){
  16. echo "
    $a1 $a2 points to the same object.";
  17. }else{
  18. echo "
    $a1 $a2 is not an object.";
  19. }
  20. ? >
Copy the code

The program running result is: The class of $a1 is A , $a2 is A $a1 $a2 point to the same object.

At this point, we have written the simplest singleton pattern. Now, you can try to write a database connection class that applies the singleton design pattern. Remember the usage and writing methods of the singleton pattern.



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