Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Paul Jones:
Configuration Values Are Dependencies, Too
Dec 09, 2015 @ 17:23:41

In this post to his site Paul Jones suggests that dependencies aren't limited to just the libraries and tools you use in your applications, it's also the configuration values that define it's environment.

As part of my consulting work, I get the opportunity to review lots of different codebases of varying modernity. One thing I’ve noticed with some otherwise-modern codebases is that they often “reach out” from inside a class to retrieve configuration values, instead of injecting those values into the class from the outside. That is, they use an equivalent of globals or service-location to read configuration, instead of using dependency injection.

[...] The effect, though, is global-ish or service-locator-ish in nature: the class is reaching outside its own scope to retrieve values it needs for its own operation. Likewise, one cannot tell from the outside the class what configuration values it depends on.

He gives code examples of this in action, showing pieces of code directly accessing the configuration structure from inside the class functionality. His examples show direct configuration access (not great), using a "Configuration" object (a bit better) and, finally, what he sees as a more correct way: injecting them like any other value. Code examples are provided for each step in this evolution, helping to illustrate his point.

tagged:

Link: http://paul-m-jones.com/archives/6203


Trending Topics: