本文整理汇总了PHP中IoC::bind方法的典型用法代码示例。如果您正苦于以下问题:PHP IoC::bind方法的具体用法?PHP IoC::bind怎么用?PHP IoC::bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IoC
的用法示例。
在下文中一共展示了IoC::bind方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_can_resolve_out_of_the_ioc_container
public function test_can_resolve_out_of_the_ioc_container()
{
IoC::bind('foo', function () {
return new Foo();
});
$this->assertInstanceOf('Foo', IoC::make('foo'));
}
示例2: getReview
{
$this->service = $service;
}
public function getReview(APIRequest $request, APIResponse $response)
{
$vars->review = $this->getPerformanceReviewService()->buildPerformanceReview($this->reviewId);
$vars->questions = $this->getPerformanceReviewService()->buildReviewQuestions($this->reviewId);
$vars->answers = $this->getPerformanceReviewService()->buildReviewAnswers($this->reviewId);
echo json_encode($this->vars);
}
public function updateReview(APIRequest $request, APIResponse $response)
{
$reviewId = $request->get('id');
try {
//The API could use a different Request object, as long as it implements the right
//interface.
$reviewAnswers = new ReviewAnswersRequest($reviewId, $request->post('questions'));
} catch (InvalidArgumentException $exception) {
//invalid data posted.
$response->errorCode(400);
$response->errorMessage('You did it wrong!');
return true;
}
$this->getPerformanceReviewService()->answerReviewQuestions($reviewAnswers);
$response->successCode(200);
return true;
}
}
IoC::bind('DB', $db);
$api = IoC::make(ReviewServiceApi::class);
$api->getReview($request, $response);
示例3: bind
//$foo = new Foo(new Bar(new Bim()));
//$foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething
class IoC
{
protected static $registry = [];
public static function bind($name, callable $resolver)
{
static::$registry[$name] = $resolver;
}
public static function make($name)
{
if (isset(static::$registry[$name])) {
$resolver = static::$registry[$name];
return $resolver();
}
throw new Exception('Alias does not exist in the IoC registry.');
}
}
IoC::bind('bim', function () {
return new Bim();
});
IoC::bind('bar', function () {
return new Bar(IoC::make('bim'));
});
IoC::bind('foo', function () {
return new Foo(IoC::make('bar'));
});
// 从容器中取得Foo
$foo = IoC::make('foo');
$foo->doSomething();
// Bim::doSomething|Bar::doSomething|Foo::doSomething