Static methods: are they still bad considering PHP 5.3 late static binding?

后端 未结 3 1419
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 16:19

If you search on the reasons why static methods are bad the first thing you find it is because you can\'t override it when you are unit testing.

So is this still tru

3条回答
  •  情深已故
    2021-01-17 17:03

    Static methods aren't bad in themselves. Sometimes certain operations don't make sense to require a particular object to do. For example, a function like square root would make more sense being static.

    Math::sqrRoot(5);
    

    rather than having to instantiate a Math 'object' and then call the function.

    $math = new Math();
    $result = $math->sqrRoot(5);
    

提交回复
热议问题