php - A certain function can be implemented using a function. Is it necessary to use a class to implement it?
滿天的星座
滿天的星座 2017-05-31 10:33:09
0
10
752

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

滿天的星座
滿天的星座

reply all(10)
大家讲道理

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.

Ty80

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...

PHPzhong

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!