drau89457 2016-03-03 14:40
浏览 34
已采纳

Silverstripe的配置API是单身人士吗?

According to the Documentation,

The Configuration API can be seen as separate from other forms of variables in the SilverStripe system due to three properties API:

Configuration is per class, not per instance.
Configuration is normally set once during initialization and then not changed.
Configuration is normally set by a knowledgeable technical user, such as a developer, not the end user.

Question is the configuration API a Singleton?

  • 写回答

1条回答 默认 最新

  • duancaiyi7567 2016-03-03 16:01
    关注

    It behaves like a Singleton, since most access to the Config API goes through Config::inst() which will always return the currently active Config instance. This instance will remain the same, until you decide to change it with Config::set_instance($myNewConfigInstance).

    So yes, there's a Singleton pattern implemented there, but you can still have multiple instances of Config (you can use this to isolate environments, like tests or whatever).

    Here's an example of how you could switch Config during code-execution:

    // preserve old config
    $defaultConfig = Config::inst();
    // create a new config
    Config::set_instance(new Config());
    
    // … do stuff that will use the new config
    
    // switch back to the default config once you're done
    Config::set_instance($defaultConfig);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?