Home  >  Q&A  >  body text

php - A certain function can be implemented using a function. Is it necessary to use a class to implement it?

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

滿天的星座滿天的星座2698 days ago756

reply all(10)I'll reply

  • 大家讲道理

    大家讲道理2017-05-31 10:35:09

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-31 10:35:09

    Several features of object-oriented: encapsulation, inheritance, polymorphism. If your needs are abstracted and have these features, you should consider using oop.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-31 10:35:09

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

    reply
    0
  • PHPz

    PHPz2017-05-31 10:35:09

    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

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-31 10:35:09

    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.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-31 10:35:09

    You can read this article
    Object-oriented and design patterns /a/11...

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-31 10:35:09

    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.

    reply
    0
  • 黄舟

    黄舟2017-05-31 10:35:09

    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

    reply
    0
  • 某草草

    某草草2017-05-31 10:35:09

    Come at your convenience

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-31 10:35:09

    Follow your own project, don’t use everything for one project, just follow the project.

    reply
    0
  • Cancelreply