This question has already been answered:

Hello, I program in php without a few months a year, and so far I can’t understand exactly what the advantages of classes are compared to functions, and why are most professionals using classes?

Explain please intelligibly what is the difference, or maybe by example

Reported as a duplicate by members vp_arth , Anton Shchyrov , user194374, ermak0ff , rjhdby Mar 3 '17 at 6:48 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • the heading and the body of the question are different ... because if you exaggerate, the class is a wrapper over functions .... but regarding the body of the question "what is the advantage and why .." - you just have to watch the literature on the PLO and classes - Alexey Shimansky

2 answers 2

This is a very deep and broad question. To paraphrase, it sounds like this: what is the difference between procedural and object-oriented programming?


In the first case, the action is put at the forefront, i.e. a set of consecutive commands to solve a specific task.

The PLO uses a modular approach, headed by an object from the real world. Those. The code is organized in such a way that it is a set of objects, each of which solves its own small task, and interacting with each other all these objects solve one common problem.


Trying to explain all this with the example of PHP

    Imagine a school.

    Pupils are variables or functions (in classes it is more correct to call methods), and school is your code. The class unites students in groups, the same thing happens in the code.

    To produce a bunch of functions ala gruppa_naznachenie1() , gruppa_naznachenie2() not very convenient. Moreover, you can make a class that will expand or change the capabilities of another class without changing it.

    Another class is convenient in that all actions will occur inside it and some unnecessary data will not break beyond it.

    And now just visually compare just procedures

    echo work_func1("blablabla").$work_var1;

    and work with the class

    $work = new WorkClass(); echo $work->func1("blablabla").$work->var1;

    Also a lot more in what is convenient. I do not want to impose any opinion on you. I personally work in small projects procedurally, without delusions of grandeur of classes. Try to write something using classes and see if you need it or not, it may come in handy sometime.