I’ve got this Config-class that I use in my PHP5 application to load & parse my static config. This far I’ve managed to keep the class from being a singleton/registry and instead passed the instance of the Config class around to wherever it’s needed with the help of Dependency injection.
But now as I need to set/make changes in my Config during runtime at one place, my changes aren’t reflected globally and being far from a specialist with the use of the static modifier in PHP, I need to ask:
What is the best way to ensure that changes to the properties in my Config-class are reflected throughout my application without making the class into a singleton? Can I just make the variable holding the config data static?
If you pass the same instance of your
Configclass to every part of your application and you change a setting it should be reflected everywhere else.In case you are creating multiple objects of the class (and parse that config file every time?!) you might want to stop doing that. It’s possibly wasteful and I’d say it is confusing.
So if you
create only oneand pass that around everything should be fine.Under the assumption that you create multiple instances of the object that only should exist once:
If you are able to “fix” your architecture to allow you to do that: Do so.
If you can’t to that… well.. creating a static property to hold your values in a class that you can have multiple instances off is, at least in my book, a major wtf factor. If you can’t fix (meaning it’s to much off a hassle to do so) just “break” it in an expected way so other people don’t trip over it.
I’d rather have a singleton than something that creates all the issues with singletons and also lies about being a wrapper for a global variable.