Home  >  Article  >  Backend Development  >  It is better to write the methods in the model statically, and it is also said that ordinary methods are better

It is better to write the methods in the model statically, and it is also said that ordinary methods are better

WBOY
WBOYOriginal
2016-12-05 13:44:281063browse

Now I see that the methods written in the model in many frameworks are static methods. When called by the controller, they can be used directly without instantiating the class before calling. How to distinguish whether it is better to use class static methods or object methods

Reply content:

Now I see that the methods written in the model in many frameworks are static methods. When called by the controller, they can be used directly without instantiating the class before calling. How to distinguish whether it is better to use class static methods or object methods

This depends on the type of your method. For example, if your method operates independently of class instances, then it can be set as a static method.
For example, in a tool class, we can set all methods in this class as static methods. At this time, the class is only used to classify our methods. If you want to use ordinary methods, then this class can also use singleton mode.

Tool classes do not require chain operations. I think they can be encapsulated into static classes.

<code>调用方便。</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