Sometimes we need to retrieve some kind of handler class that depending on an attribute is different.
Thanks to Factory pattern we can solve this ...
For further actions, you may consider blocking this person and/or reporting abuse
I love this pattern ! It's really clean.
You could even simplify it using
tagged_iterator
.Thanks to DI feature, you do not have to declare anything manually !
The idea come from Thibault Richard (github.com/t-richard) :)
Wow! looks awesome! even better, thanks for sharing
I think you could update your post with it ;)
Done! many thanks :)
How about?
And this one ? ^^
You can also take it one step further, avoid tags and any YAML coding by using annotation autowire:
Yep, but then you are placing something from the Framework (infrastructure layer) to the factory (domain layer).
It's also a valid approach, but I prefer keeping things separated (depends on the architecture of your app, of course).
Anyway, thanks for the idea!
Hi, how do you use those class property in a static method? I try to call
$this->channels
and my IDE show errorCannot use '$this' in non-object context
.And do I still need to inject the factory class or I can just directly use it like
ChannelFactory::create()
?This was an error! just fixed... this shouldn't be a static class, we want to use service container to manage that data. Thanks & fixed!
I would suggest minor optimization.
Do not create new
$channel
, since all channels are already initialized.Just return it: