How is testing the registry pattern or singleton hard in PHP?

前端 未结 3 1646
小蘑菇
小蘑菇 2020-11-30 02:56

Why is testing singletons or registry pattern hard in a language like PHP which is request driven?

You can write and run tests aside from the actual program executio

3条回答
  •  半阙折子戏
    2020-11-30 03:20

    Singletons (in all OOP languages, not just PHP) make a particular kind of debugging called unit testing difficult for the same reason that global variables do. They introduce global state into a program, meaning that you can't test any modules of your software that depend on the singleton in isolation. Unit testing should include only the code under test (and its superclasses).

    Singletons are essentially global state, and while having global state can make sense in certain circumstances, it should be avoided unless it's necessary.

提交回复
热议问题