design patterns - How to avoid clashing PHP traits used for dependency injection -
I'm getting around to finding symptoms in PHP at first place I thought I'd try it in config bits in class Is injected. If I am using DIC, then I may have such code in any class that requires config object:
Secure function setConfig ($ config) {$ This- & gt; Config = $ config; } Protected $ config; It seems that in order to avoid boilerplate code at all places, it is a natural fit for these symptoms, so I can make it:
Trait Config {protected function SetConfig ($ config) {$ this- & gt; Config = $ config; } Protected $ config; } And then use it like this:
square fu {use the config; Public Function __construct () {// Now $ - this-> Config}}. This is very good Now we say that I want to create a second property, for logging:
Attribute logger {secure function to logger ($ logger) {$ this-> logger = $ logger; } Protected $ logger; } How can I use it:
square fu {use logger; Public Function __construct () {// now $ this- & gt; Logger}} can also make great use. Now the problem is that if both of these properties want to use each other it seems quite reasonable that a logger class is injected with a config The object will be required, which means that: attribute logger {config config; Protected work from Logger ($ logger) {$ this- & gt; Logger = $ logger; } Protected $ logger; } But then things will be broken when another class uses both of these properties:
class Foo {config, logger; Public function __construct () {// $ this- & gt; Config and $ this- & gt; Logger}} This definitely does not work because the configuration bits are effectively duplicated in FU.
I just use the configure from logger properties; could leave the piece, knowing that it would be in the end but it sounds strange to me because it makes a type of external dependency what will happen if I want to use logger to use the location which is already config Not a feature? This solution also means that I need to warn my IDE (PhpStorm 8) about unknown methods, and not providing autocomplete. I know that I can fix these problems using @Mithew, but it's just putting lipsticks on a pig. I can also call config bits in logger, but it is also problematic. / P>
It does all this to smell, but I still do not know that this is because it is a new pattern for me or if it is actually a stinking pattern, in any way, I'm not sure This approach is the best way to actually work.
What is the best way to solve this problem in symptoms? Or is it better to avoid DIC shortcutting symptoms?
The method I have found is useful, as well as the setters, then it allows you to enter a particular type The person needs to be present without any conflict with other characteristics.
trait Config {protected function SetConfig ($ config) {$ this- & gt; Config = $ config; } Protected function GetConfig () {Return $ this- & gt; Config; } Protected $ config; } Attribute logger {abstract protected function GetConfig (); Protected work from Logger ($ logger) {$ this- & gt; Logger = $ logger; } Protected $ logger; } Class Buzz {Config, Logger; In the falcon, the config attribute provides the necessary abstract method and the fen arc is made without error. If you accidentally use logger then you will get a serious error. Find: Class Baza contains 1 abstract method and therefore it is summarized or remaining remaining methods (Falcon :: GetConfig)
Comments
Post a Comment