Dependency Injection
- General
Dependency Injection
What is dependency injection?
To understand dependency injection first we need to know about dependency. Let’s take an example.
Class User {
Private Profile $profile;
Public user(){
$this.profile = new Profile();
}
}
Here we have a class User. which has another class Profile object as variable. So We can say that user class is dependent on profile class. In this example user class has a constructor which is creating object of profile class.
Dependency injection is a technique whereby one object supplies the dependencies of another object.
Now just modify above example like that –
Class User {
Private Profile $profile;
Public user(Profile $profile){
$this.profile = $profile;
}
}
Here User class is dependent on Profile class. But it is not instantiate profile object itself. It just out source dependency and when ever we will instantiate user class we need to instantiate profile class also.
Like this –
$profile = new Profile();
$user = new User($profile);
Type of dependency injection
- By Constructor – In this type we pass or inject dependency by constructor. like our example.
- By Setter method – In this type we pass dependency to setter method. For example –
Class User {
Private Profile $profile;
Public setProfile(Profile $profile){
$this.profile = $profile;
}
}
Here we are passing dependency by setter method.
$user = new User();
$user->setProfile(new Profile());
This is how we pass it.
Dependency Injection Container
In our examples we are resolving dependencies manually. What if our application automatically handle the dependencies for a specific class instead of doing it manually.
DI container is a class or application who resolve dependencies automatically.
Look at a DI container below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
<?php /** * Class Container */ class Container { /** * @var array */ protected $instances = []; /** * @param $abstract * @param null $concrete */ public function set($abstract, $concrete = NULL) { if ($concrete === NULL) { $concrete = $abstract; } $this->instances[$abstract] = $concrete; } /** * @param $abstract * @param array $parameters * * @return mixed|null|object * @throws Exception */ public function get($abstract, $parameters = []) { // if we don't have it, just register it if (!isset($this->instances[$abstract])) { $this->set($abstract); } return $this->resolve($this->instances[$abstract], $parameters); } /** * resolve single * * @param $concrete * @param $parameters * * @return mixed|object * @throws Exception */ public function resolve($concrete, $parameters) { if ($concrete instanceof Closure) { return $concrete($this, $parameters); } $reflector = new ReflectionClass($concrete); // check if class is instantiable if (!$reflector->isInstantiable()) { throw new Exception("Class {$concrete} is not instantiable"); } // get class constructor $constructor = $reflector->getConstructor(); if (is_null($constructor)) { // get new instance from class return $reflector->newInstance(); } // get constructor params $parameters = $constructor->getParameters(); $dependencies = $this->getDependencies($parameters); // get new instance with dependencies resolved return $reflector->newInstanceArgs($dependencies); } /** * get all dependencies resolved * * @param $parameters * * @return array * @throws Exception */ public function getDependencies($parameters) { $dependencies = []; foreach ($parameters as $parameter) { // get the type hinted class $dependency = $parameter->getClass(); if ($dependency === NULL) { // check if default value for a parameter is available if ($parameter->isDefaultValueAvailable()) { // get default value of parameter $dependencies[] = $parameter->getDefaultValue(); } else { throw new Exception("Can not resolve class dependency {$parameter->name}"); } } else { // get dependency resolved $dependencies[] = $this->get($dependency->name); } } return $dependencies; } } |
This container is registering each class via set method.
$container = new Container();
$container->set(“User”);
Whenever we want to instantiate User we need to call get method of container.
$user = $container->get(“User”);
get() function is going through resolve() function, which is checking if the User class has any dependencies via its __construct() so it will resolve them recursively (means if Profile class has dependencies, they will be resolved as well), otherwise, it will instantiate User class direct.
DI container use ReflectionClass. The ReflectionClass class reports information about a class. You can read about it here.
Conclusion – Dependency Injection is passing of dependency to object. To do it manually we need to use a DI container. Who will Autowired the dependency for application.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s