Home > Backend Development > PHP Tutorial > 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
Release: 2023-03-03 11:02:02
Original
1147 people have browsed it

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>
Copy after login
Related labels:
php
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