vdegenne
vdegenne

Reputation: 13298

is it ok to have large class definition?

I like having my Objects very polyvalent. I code functions that I don't even use in the main purpose program, just in case my program get more complex. My classes hence get quickly large in height of lines. But what about the performance ? I have no idea how php treats my files. Let's say i have 30 functions in my class, when an Object is instanciated from that class just for the use of 1 function in it, how is the whole code handled ? is all the logic loaded in the memory ? or does PHP anticipate and only build the used functions and data ?

In other words, is it okay to have very large files ?

Upvotes: 0

Views: 75

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

No, PHP doesn't anticipate what parts of a file or class you're going to use before you've used them, how can it know in advance?

When you load a class the entire class is resident in memory. Of course, code is only loaded once per request; so you only ever have one copy of the class methods in memory at a time, no matter how many object instances you create. However, if you're defining a great many more properties, then those properties are taking up memory on a per-instance basis

But having large classes with a great many methods that you don't use is generally considered bad practise, and contrary to the principles of SOLID. Large, monolithic classes are harder to maintain than small, more-refined classes that serve very specific purposes

Upvotes: 2

Related Questions