I use PHP. Most of the functions in the project I am writing now are encapsulated into functions. When needed, just require and then pass parameters to call the function. I don’t know if this will be bad. , do we still need to use classes to implement it?
Take the homepage of the segmentfault website as an example (the homepage displays some questions). If you want me to write (using MVC), I will first write a function getQuestion(parameter) that specifically gets the questions, and then add it to the homepage model (class ) in the require function, and then call this function by passing parameters. After obtaining the data, render it to the View.
I always feel that "require this function and then call it to get the data" is not good to write, but I don't know how to write it well. I hope you can give me some advice
In fact, it doesn’t matter, but if you want to ask for an optimal solution, a standard solution, then use the
class
to implement it.Perhaps you still don’t understand many concepts and your understanding is vague, so first follow the recommended practices and wait for a while to take a look.
Several features of object-oriented: encapsulation, inheritance, polymorphism. If your needs are abstracted and have these features, you should consider using oop.
Your question is equivalent to asking which is better, object-oriented or process-oriented. This issue has been debated for many years. Nowadays, some people also say function-oriented. Everyone has their own favorite way
My opinion is based on specific analysis of specific situations. Object-oriented is easy to reuse, easy to expand, and easy to maintain, but process-oriented has higher performance than object-oriented. If the project has the same logical modules or there are many duplicate codes, it is recommended to use object-oriented. If the project If the structure is not complex and performance is pursued, then process-oriented is more recommended. In fact, these two methods can be used in combination in a project. Depend on specific needs
http://m.blog.csdn.net/articl...
Large projects should be implemented using classes as much as possible, while small projects are easier to use functions
Those that need to be reused should be implemented through classes as much as possible
The advantage of using classes is that if you write too much code, it will be easier to write new projects after modularization
To be truly object-oriented, you must have:
stateful
Communicate with messages (message passing)
Otherwise consider functional or procedural.
It is also possible to treat a class as a namespace. For example, a class is full of static methods.
But
require
itself is an implementation of namespace, so it is not needed.You can read this article
Object-oriented and design patterns /a/11...
Without a doubt, use OO programming.
The PHP project has developed to version 7, which is already very OO. There is no need to reverse history.
Pursuing performance? Don't be ridiculous, the performance bottleneck of web applications is generally not here.
For the require issue you mentioned, you can just use composer + namespace.
It is recommended that you learn framework codes such as symfony and laravel, and stop working behind closed doors.
Design pattern is not limited to the implementation method, it is just an idea, just follow your personal preference.
I personally like to use a static class (all static methods in the class) to handle this scenario
Come at your convenience
Follow your own project, don’t use everything for one project, just follow the project.