Last active
February 16, 2022 16:37
-
-
Save mindplay-dk/94b647442c2072866e42 to your computer and use it in GitHub Desktop.
Minimal DI container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Container | |
{ | |
/** @var Closure[] */ | |
private $factories = []; | |
/** @var array */ | |
private $components = []; | |
public function get($id) | |
{ | |
if (!isset($this->components[$id])) { | |
$this->components[$id] = call_user_func($this->factories[$id], $this); | |
} | |
return $this->components[$id]; | |
} | |
public function set($id, $value) | |
{ | |
$this->components[$id] = $value; | |
} | |
public function register($id, Closure $func) | |
{ | |
$this->factories[$id] = $func; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment