Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- App.php
- <?php
- namespace core;
- class App {
- protected static $container;
- public static function setContainer($container){
- static::$container = $container;
- }
- public static function Container(){
- return static::$container;
- }
- public static function resolve($key){
- return App::$container->resolve($key);
- }
- }
- Container.php
- <?php
- namespace core;
- class Container{
- protected $bindings=[];
- public function bind($key, $resolver){
- $this->bindings[$key] = $resolver;
- }
- public function resolve($key){
- if (!array_key_exists($key, $this->bindings)){
- throw new \Exception('No matching binding found for ' . $key);
- }
- $resolver = $this->bindings[$key];
- return call_user_func($resolver);
- }
- }
- Bootstrap.php
- <?php
- use core\App;
- use core\Container;
- use core\Database;
- use core\Session;
- $container = new Container();
- $container->bind('core\Database', function (){
- $config = require base_path("src/config.php");
- return new Database($config['database'], $config['database']['user'], $config['database']['password']);
- });
- $container->bind('core\Session', function(){
- return new Session;
- });
- App::setContainer($container);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement