Does calling dozens of classes in a system noticeably influence the performance of the application?

Asked

Viewed 142 times

3

I’m starting to work with object orientation in PHP and would like to know if the amount of existing classes in a system can interfere in the performance of it or if it would have to be hundreds of those classes so that actually the performance could start to be affected.

  • 3

    The number of classes does not influence no, the instances are that occupy memory and the algorithms processors.

4 answers

7

The amount of classes makes no noticeable difference, what you do in them can make a difference. Of course it makes a difference, but that’s not why you should use classes or not.

The term "call classes" doesn’t even make sense.

Classes are used to organize code, so use them to organize, or stop using if this organization has no advantage. And in PHP it is common not to bring advantage, there is an excessive use of classes in most codes. In general the person uses a class because he does not know what he is doing, he just follows the flow.

It is okay to create classes when they will bring something useful, if one can clearly and individually justify their need. If she can’t do this - and it’s not enough to give a generic justification like "will organize the code" - it’s because the class isn’t needed.

So the concern should not be this, and if it is, you should probably change language, since PHP is not a language to be fast, it is to develop fast. This shows how strange it is to use classes in the language, since it refers to OOP which is a better architected development and which would be useful in complex systems, not in systems of scripts.

The right one depends on the context. Anyone who says things of this kind that are right for one side or the other in general terms is just parrot. You have to analyze the problem to decide what to do. Experience counts in these cases. When you don’t have it, you have to ask for help, but you have to ask in terms that you can do an analysis, it’s not easy, and mostly it doesn’t usually fit here, unless you know how to ask.

It is common for a person to opt for a form without plausible justification. He chooses a form and goes into it without thinking.

Some questions that may help to better understand the subject:

  • 2

    +1 by the answer, demystifies facts that "window programmers only" do not understand. I think it’s extremely important to teach things that work in the real world here at Sopt. Only this paragraph would eliminate many absurdities I see in PHP if people understand: "It is okay to create classes when they will bring something useful, if one can clearly and individually justify their need. If she can’t do this - and it’s not enough to give a generic justification like "will organize the code" - it’s because the class isn’t needed."

  • @Maniero in this context " since PHP is not a language to be fast, it is to develop fast." which web language (with cli mode support) would be compatible with this scenario (speed/performance) in your opinion ?

  • 1

    @Mcunha98 The fast languages that are used for performance are C# first and Java second. Others are also used, but little, of course, knowing how to do C++ or Rust can be faster, but few venture, it’s too much work, and if you do it wrong you get slower (there are several cases of C# faster than C++, C++ only gets faster with a lot of effort. Essentially any language supports CLI, some more than others, c# supports very well. Interestingly the way people do today in PHP nor develop fast is rolling over :D

7

Speaking more of the same, it makes no difference. The explanation is in the other answers.

A practical point to be aware of is how these classes are loaded.

You will probably want to use an autoloader. Then yes, in this case will consume processes that greatly influence performance.

An autloader is to include a file when it invokes a class that is not present in the body of the script. This process of including the file consumes a lot of memory and processing.

To reduce this cost, it is recommended to create build cache structures. We can also choose or reinforce with cache libraries like Memcache, APC, Opcode, among others.

However, if the concern is whether the use of OOP classes or concepts will make the system less performatic than writing the codes in procedural style, obviously, the procedural style is much faster. However, think this way, you need to go from your home to the corner bar that is 20 meters away. How will you go?

1. a pé
2. de moto
3. de lamborguini

I’ll believe you chose option 1.

Now, let’s say you want to go from your home to Paris.

1. a pé e segue nadando pelo oceano
2. de moto e segue de barco a remo pelo oceano
3. vai até um aeroporto e segue de avião

I think you opted for the plane.

But why is a plane that consumes much more resources and is heavy? Can’t you still walk and continue swimming in the ocean? After all, it gives the same result.

Think of this analogy or similar analogies and you will understand when to use a style or conceitY.

Always think about the ultimate goal. What cost/benefit option X or Y will generate?

Analyzed cost/benefit, think about effectiveness.

The cost/benefit is effective?

These are basic steps to make a more appropriate choice.

  • :) Excellent answer. It is variable and we must analyze the best way to apply some style or concept. The example shown is very specific and direct to the question.

-1

Performance will depend on how classes are used, how they are written, how they behave, and how they relate, in addition to the server features that host your application. Simply having class in the system does not affect performance.

-4

Just to reinforce, a very important context for OOP is to use Estatic classes when your goal is to limit the functionality of an object (thus not getting a lot of generic functions in the code), bring organization to your code without the need to instantiate

  • 2

    Your answer was very vague and meaningless. Could you explain this better? Why is grouping functions in a static class better in OOP? By the way, what is the relationship between static class and OOP? If functions are said to be generic when defined outside the class, why would they no longer be generic within the class? Why does a static class improve organization? What’s the difference between having a file with a class of methods and a file with functions?

  • The context is simple, for example grouping date-related functions into a static class dealing with date (add days, convert formatted, validate, etc...) of course you can stick everything in one file and pray to remember the names of the functions or then encapsulate the operators in static classes that connect to the context ...by example Classedata::validate($value), Classedata:::addirDias($value)etc... again, a matter of approach, you can do everything in a general archive too , goes from the level of organization you want to achieve

  • But I can’t understand how ClasseData::validar can be more organized than validar_data. When reading the function name I know exactly what it does and I don’t need to worry about any class.

  • Again...it depends on the level of organization, I am not saying that It is right and I am wrong (or vice versa), only that operations limited to a scope can be better mapped/understood when they are below something more representative

Browser other questions tagged

You are not signed in. Login or sign up in order to post.