开发者

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

开发者 https://www.devze.com 2023-03-20 04:41 出处:网络
Right now I have a normal class like this class MyClass { function myfunc1 () { } function myfunc2 () { }

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,开发者_如何学Python 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?


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.


You want to split functionality up in different classes and files. For example, have 1 file per class, and have a single class have a clear purpose instead of one big god class.


You probably shouldn't use several files for the same class. If the class is getting to big to manage, it's likely that you should be using subclasses.

0

精彩评论

暂无评论...
验证码 换一张
取 消