PHP code example of fanqingxuan / container

1. Go to this page and download the library: Download fanqingxuan/container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fanqingxuan / container example snippets




use JsonContainer\Container;

$container = new Container;

class HomeController {

    private $userService;

    public function __construct(UserService $userService) {
        $this->userService = $userService;
    }

    public function say() {
        return $this->userService->getUser();
    }
}

interface UserService {

    public function getUser();
}

class UserServiceImpl implements UserService {

    private $userDao;

    public function __construct(UserDao $userDao) {
        $this->userDao = $userDao;
    }

    public function getUser() {
        return $this->userDao->getUser();
    }
}

class UserDao {

    public function getUser() {
        return "this is userdao/getuser";
    }
}

$container->bind(HomeController::class,HomeController::class);
$container->bind(UserService::class,UserServiceImpl::class);
$container->bind(UserDao::class,UserDao::class);

$homeController = $container->make(HomeController::class)->say();
print($homeController);



use JsonContainer\Container;

$container = new Container;

class Test {

}

$container->bind("test",function() {
    return new Test;//返回实例对象
});

$container->bind("hello",function() {
    return "hello world";//返回字符串
});



use JsonContainer\Container;

$container = new Container;

class Test {

}

$container->singleton("test",function() {
    return new Test;
});



use JsonContainer\Container;

$container = new Container;

class Test {

}

$container->instance("test",new Test);



use JsonContainer\Container;

$container = new Container;

class HomeController {

    private $id;

    public function __construct($id) {
        $this->userService = $userService;
        $this->id = $id;
    }

    public function say() {
        return $this->id;
    }
}

$value = 32;
$container->when(HomeController::class)
          ->needs('$id')
          ->give($value);

$this->app->bind(
    Cache::class,
    RedisCache::class
);

class HomeController {

    private $userService;

    public function __construct(UserService $userService) {
        $this->userService = $userService;
    }

    public function say() {
        return $this->userService->getUser();
    }
}

interface UserService {

    public function getUser();
}

class UserServiceImpl implements UserService {

    private $userDao;

    public function __construct(UserDao $userDao) {
        $this->userDao = $userDao;
    }

    public function getUser() {
        return $this->userDao->getUser();
    }
}



use JsonContainer\Container;

$container = new Container;


class Cache {

}

class Memcache extends Cache {

}

class Redis extends Cache {

}

class UserService {

    private $cache;

    public function __construct(Cache $cache) {
        $this->cache = $cache;
    }

    public function say() {
        print_r($this->cache);
    }
}

class OrderService {

    private $cache;

    public function __construct(Cache $cache) {
        $this->cache = $cache;
    }

    public function say() {
        print_r($this->cache);
    }
}

$container->when(UserService::class)
          ->needs(Cache::class)
          ->give(function () {
              return new Memcache;
          });

$container->when([OrderService::class, ImageService::class])
          ->needs(Cache::class)
          ->give(function () {
              return new Redis;
          });

print_r($container->make(UserService::class)->say());
print_r($container->make(OrderService::class)->say());

class Cache {

}

class Memcache extends Cache {

}

class Redis extends Cache {

}

$container->bind("memcache",function() {
    return new Memcache;
});

$container->bind("redis",function() {
    return new Redis;
});

$container->tag(['memcache','redis'],'cache');

$cachelist = $container->tagged('cache');

foreach ($cachelist as $cache) {
    print_r($cache);
}

class Cache {

}

class Memcache extends Cache {

}

class Redis extends Cache {

}

$container->bind("cache",function() {
    return new Memcache;
});

$container->extend("cache",function($serivce) {
    //$service是Memcache对象
    return new Redis;
});

print_r($container->make("cache"));

class Cache {

}

class Memcache extends Cache {

}

$container->bind(Memcache::class,Memcache::class);

print_r($container->make(Memcache::class));

class Test {
    
}

class Cache {

    private $test;
    private $id;

    public function __construct(Test $test,$id) {
        $this->test = $test;
        $this->id = $id;
    }

    public function get() {
        return $this->id;
    }

}

$container->bind(Cache::class,Cache::class);

print_r($container->makeWith(Cache::class,['id'=>33])->get());
print_r($container->make(Cache::class,['id'=>33])->get());




use JsonContainer\Container;

$container = new Container;


class Test {
    
}

class Cache {

    private $test;

    public function __construct(Test $test) {
        $this->test = $test;
    }

}

$container->bind("test",Cache::class);
//解析所有服务都会触发这个事件
$container->resolving(function ($object, $container) {
    print_r($object);
    print_r($container);
});
//当解析cache时会触发这个事件
$container->resolving("cache", function ($object, $container) {
   print_r($object);
});

$container->make("test");


class Test {
    
}

class Cache {

    private $test;

    public function __construct(Test $test) {
        $this->test = $test;
    }

}

$container->bind("cache",Cache::class);

print_r($container->get("cache"));
print_r($container->has('cache'));


class Cache {

    public function __construct() {
    }

}

$container->bind("cache",Cache::class);

print_r($container['cache']);
unset($container['cache']);

class Test {
    private $id;
    public function __construct($id) {
        $this->id = $id;
    }
}

class Cache {
    public function __construct(Test $test) {
    }
}

$container->bind("cache",Cache::class);
$container->Test = function () {
    return new Test(333);
};
var_dump($container->cache);

$container = Container::getInstance();

$container->flush();

$container->has("cache");
isset($container['cache'])

$container->isShared("cache")