當前位置: 首頁>>代碼示例>>PHP>>正文


PHP IoC::bind方法代碼示例

本文整理匯總了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'));
 }
開發者ID:actank,項目名稱:simple-di-container,代碼行數:7,代碼來源:IoCTest.php

示例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);
開發者ID:jlesueur,項目名稱:service-examples,代碼行數:31,代碼來源:ReviewServiceAPI.php

示例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
開發者ID:breezelife0,項目名稱:mycode,代碼行數:31,代碼來源:simple-di-container.php


注:本文中的IoC::bind方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。