In PHP, is there any performance impact on using one long class with a lot of functions in it? Or is it advisable to use many small classes and call them separately when needed?
I am a newbie in OOPS and please ignore any silliness in the question. Thanks.
Classes with a single responsibility are better because they are easier to extend and easier to test. They are more reusable than complicated classes.
A single Java program contains two or more classes, it is possible in two ways in Java.
"There is a point at which small class sizes do not produce better outcomes. They produce worse outcomes," says author Malcolm Gladwell about teaching teenagers.
It's advisable not to think about the performance before you have the code. From the maintainability and understandability viewpoint, of course, smaller classes, with smaller methods are superior. (see The Single Responsibility Principle)
When you need to optimize, you really can assemble (automatically) all your code into a single big file, and save some time on include-s.
First, think about clean OOP design.
Very long classes with a lot of methods and properties are very difficult to understand.
A set of well-designed classes with meaningful class names, method names and variable names are very easy to understand especially when it comes to maintenance period.
In general, people tend to make classes that are too large and complex -- this is because it's often difficult to see where the cohesion boundaries lie. So when you're starting out and a bit unsure, it's better to err on the side of making them too small: you can always coalesce smaller classes into a larger one... it's often much harder to refactor a large class into several small ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With