Home  >  Article  >  PHP Framework  >  Singleton mode of ThinkPHP container

Singleton mode of ThinkPHP container

咔咔
咔咔Original
2020-10-10 11:21:592664browse

This article mainly focuses on the implementation of the internal containers and facades of the framework as a benchmark,

Preface

I have analyzed the source code analysis of automatic loading of classes and configuration file loading before. This article is the third article, which mainly analyzes the source code for the implementation of containers and facade classes. And learn some knowledge points to implement this function.

1. Singleton mode

is learning There are two design patterns that you need to understand before containers and facades, singleton mode and registration tree mode.

First give a brief explanation of the singleton mode.

  • Have a constructor and the attribute is private
  • Have a static member variable to save the instance of the class
  • Have a static method to access this instance

This is a simple singleton mode implemented by Kaka. Check the above three features to see if they are consistent.

The static variable is instance

It has a structure and is private

The last one is the static method getInstance

Singleton mode of ThinkPHP containerLet’s do a simple test

Still testing in the index controller. In order to confirm that its class has only been instantiated once, it has been called four times.

Singleton mode of ThinkPHP containerVisit this method to take a look

new-class is executed only once, which directly proves that the created class is only instantiated once. Singleton mode of ThinkPHP containerA question I had before Kaka here is why the constructor here uses private attributes.

Have you ever had this question before? Kaka will take you to answer it

The constructor method of defining private properties in this class is to prevent its class from being instantiated externally.

When this class is instantiated externally, the error shown below will be reported.

Singleton mode of ThinkPHP containerSo why do I mention the singleton model here! This is because

will be used in the source code of the next learning container. For example, in the thinkphp/library/think/Container.php class below, there is an instance of getting the current container.

Singleton mode of ThinkPHP containerUp to this point, the singleton mode has been briefly understood. Understanding the singleton mode is also to better understand the container.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always adhered to since its beginning. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of Singleton mode of ThinkPHP container. For more information, please follow other related articles on the PHP Chinese website!

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