Search code examples
phpscriptingprogramming-languages

PHP: Best way to write a Class where its functions can be added infinitely "as you go"?


Right now I have a normal class like this

class MyClass
{
   function myfunc1 () { }
   function myfunc2 () { }
}

However the filesize of MyClass is becoming large as I add more functions along the way. Everytime I use MyClass, only selected functions will be used by each individual application.

What is the best way to minimize the filesize of MyClass? Is it advisable to separate each functions in a separate file, and just include them if they're needed? If yes, how do you implement it in terms of code?


Solution

  • Infinitely growing classes generally lead to god objects, which are considered an anti-pattern in OO software development. When designing a class, first think about what this class is supposed to do for you, then you can design it's interface (aka public methods). If you have a good design, it's unlikely your class grows by much later on. If it does, it's time for a refactorization and breaking the class into smaller pieces.