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

前端 未结 3 1645
小蘑菇
小蘑菇 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:30

    When finishing a PHP test, you can flush singleton instance like this:

    protected function tearDown()
    {
        $reflection = new ReflectionClass('MySingleton');
        $property = $reflection->getProperty("_instance");
        $property->setAccessible(true);
        $property->setValue(null);
    }
    

提交回复
热议问题