Dependency Injection
Why Stack Overflow recommends it
Stack Overflow users link it when answering PHP and OOP design questions where a newcomer needs a quick, beginner-friendly introduction to dependency injection.
Recommended by 3 people across 3 different months, 2013 to 2017.
What it is
A short 2013 explainer by Anthony Ferrara (ircmaxell) on dependency injection in object-oriented PHP code. It argues that you do not need a fancy container and that injecting dependencies manually is simple.
What people said
Recommends tereško · 2013-09 · Stack Overflow · on “Pass in the connection object or create it inside class? (OOP)”
Maybe this one [this video] . It's kinda hard to get any good and newbie-friendly materials on subject. It's not like you can throw Fowler [LINK] at them and expect to understand =/
Recommends Bryan · 2014-05 · Stack Overflow · on “ORM Design with foreign keys in PHP”
I would say the other way around makes more sense, ie a customer has orders. an order does not have customers. As for instantiating the object inside the other is debatable. You might find this video on dependency injection a good watch, [this video]
Recommends Noctis · 2017-05 · Stack Overflow · on “How to expose application in a custom class file in WPF”
class foo is your custom class, and it gets injected whatever dependencies it needs. Google it up, do some reading, there's even a video on youtube [this video] (i didn't watch it, but i guess it'll give you an idea)
Cites Orangepill · 2013-10 · Stack Overflow · on “Dynamically include variables php”
It appears to me you may be going about this the wrong way. But first off the problems with the code you presented. Your code appears to be asking for two parameters but you are only passing in one. This will make your is_dir / is_file calls always fail because both of the conditions will never be true at the same time so your file isn't include. Also you don't appear to be assigning the returned value to anything that has a lifetime greater then that of run time of your static function so even if the file was included your config variable would end up being thrown away. To clean up your existing code you could do something along these lines. <?php class Config { static public $config; publi
Cites ircmaxell · 2014-04 · Stack Overflow · on “How to keep track of the root object from a class chain in PHP”
So, in the spirit of answering the question, of course there's a way (warning: here be dragons ): function YouAreAnIdiotIfYouDoThisForReal($skip = 0) { $bt = debug_backtrace(); if (isset($bt[1]['function']) && $bt[1]['function'] === "__construct") { // called from constructor, so skip it! $skip++; } foreach ($bt as $stack) { if (isset($stack['function']) && $stack['function'] === "__construct" && $skip-- <= 0) { return $stack['object']; } } return null; } Which can be used as so: class Header { public function __construct() { $this->project = YouAreAnIdiotIfYouDoThisForReal(1); $this->modules = YouAreAnIdiotIfYouDoThisForReal(); } } Now, this does what you ask. But please, under NO CIRCUMSTA
Cites chopper draw lion4 · 2014-11 · Stack Overflow · on “Rails: Is Gemfile a Dependency Injection Container (DIC)?”
I am learning about Dependency Injection, which from my understanding is simply a means of managing dependencies and decoupling dependencies between classes and so fourth. According to Anthony Ferrara [this video] one way to achieve dependency injection is to use a dependency injection container (DIC). DIC's do the following: 1) List all dependencies needed 2) Have logic to instantiate dependencies My question is this: Is Ruby on Rails Gemfile a DIC?
Cites aretecode · 2015-02 · Stack Overflow · on “Correct use of dependency injection container”
Similar to what CJD said, if your DIC is available everywhere , then it would be more like a Global Registry [LINK] In the case of the DB class, if it's dependencies are communicated via type hinting the parameters, defining them externally, in the documentation, or any other acceptable way, then the DIC can inject those dependencies. Helpful resources: Anthony Ferra/ircmaxell video on Dependency Injection [this video] Fabian Potencier article about whether you need a DIC [LINK] Rob Allen video Introducing Dependency Injection [LINK] Stephan Hochdorfer video The 7 Deadly Sins of Dependency Injection [LINK] Martin Fowler: Injection [LINK] InversionOfControl [LINK] DIP In The Wild [LINK] StackOverf
Cites ircmaxell · 2015-04 · Stack Overflow · on “Converting this Encrypt - Decrypt PHP Class into a full static PHP class”
@WilliemBabalola then going static is the wrong solution. The better one would be to instantiate once, and pass that object using dependency injection [this video] .
Cites Félix Adriyel Gagnon-Grenier · 2016-07 · Stack Overflow · on “Implementing dependency injection with parent classes?”
You will actually need to implement dependency injection then. Presently there is none; no object are being passed their dependencies. In your constructors, you should be receiving the objects needed for correct execution. It's the basis of dependency injection [this video] . Dependency injection would happen at the bootstrap phase [LINK] of an application. I think of bootstrap as the action of preparing the environment to actually handle whatever it is called to do, in our case handling a http request. Generally an index.php only calls a bootstrap.php file [LINK] . That bootstrap file will route the request to a specific controller, creating dependencies along the way. Using parent::__construct(