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


PHP Container::bind方法代碼示例

本文整理匯總了PHP中Container::bind方法的典型用法代碼示例。如果您正苦於以下問題:PHP Container::bind方法的具體用法?PHP Container::bind怎麽用?PHP Container::bind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Container的用法示例。


在下文中一共展示了Container::bind方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testCanBindInterfaceToImplementationUsingBind

 public function testCanBindInterfaceToImplementationUsingBind()
 {
     $this->container->bind('NetRivet\\Container\\Test\\BarInterface', 'NetRivet\\Container\\Test\\Bar1');
     $foo = $this->container->make('NetRivet\\Container\\Test\\Foo');
     $bar = $foo->getBar();
     $this->assertInstanceOf('NetRivet\\Container\\Test\\Bar1', $bar);
 }
開發者ID:netrivet,項目名稱:container,代碼行數:7,代碼來源:ContainerTest.php

示例2: setUp

 /**
  * Setup the tests
  */
 public function setUp()
 {
     $this->app = IlluminageServiceProvider::make();
     $this->app->instance('path.public', 'tests/public');
     // Bind mocked config
     $this->app->bind('config', function () {
         return Mockery::mock('config', function ($mock) {
             $mock->shouldReceive('get')->with('illuminage::image_engine', '')->andReturn('Gd');
             $mock->shouldReceive('get')->with('illuminage::quality', '')->andReturn(75);
             $mock->shouldReceive('get')->with('illuminage::cache_folder', '')->andReturn('');
         });
     });
     // Create some dummy instances
     $this->image = $this->app['illuminage']->image('foo.jpg');
     $this->thumb = $this->app['illuminage']->thumb('foo.jpg', 100, 100);
 }
開發者ID:anahkiasen,項目名稱:illuminage,代碼行數:19,代碼來源:IlluminageTestCase.php

示例3: bind

class Container
{
    protected $binds;
    protected $instances;
    public function bind($abstract, $concrete)
    {
        if ($concrete instanceof Closure) {
            $this->binds[$abstract] = $concrete;
        } else {
            $this->instances[$abstract] = $concrete;
        }
    }
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}
$container = new Container();
$container->bind('superman', function ($container, $moduleName) {
    return new Superman($container->make($moduleName));
});
$container->bind('xpower', function ($container) {
    return new Xpower();
});
$superman_1 = $container->make('superman', ['xpower']);
var_dump($superman_1);
var_dump($superman_1 instanceof Superman);
開發者ID:Justin1989,項目名稱:Toolkit,代碼行數:31,代碼來源:IoC.php

示例4: testBindReadOnly

 /**
  * @covers            ::bind
  * @expectedException RuntimeException
  */
 public function testBindReadOnly()
 {
     $this->container->setReadOnly(true);
     $this->container->bind('something', $value);
 }
開發者ID:fuelphp,項目名稱:display,代碼行數:9,代碼來源:ContainerTest.php

示例5: Container

<?php

namespace RomanNumerals;

error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
$routes = [['GET', '/api/generate/{arabicNumber:\\w+}', 'RomanNumerals\\ConvertController@generate'], ['GET', '/api/parse/{romanNumber:\\w+}', 'RomanNumerals\\ConvertController@parse']];
$container = new Container();
$container->bind('RomanNumerals\\ConvertController', function () {
    $object = new ConvertController();
    $object->setRomanNumeralGenerator(new RomanNumeralGenerator());
    return $object;
});
$container->bind('RomanNumerals\\Controller', function () {
    return new ConvertController();
});
$app = new Application($container, new Router($routes));
$app->start();
開發者ID:alexlvcom,項目名稱:RomanNumeralGenerator,代碼行數:19,代碼來源:index.php

示例6: make

        }
    }
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}
// 創建一個容器(後麵稱作超級工廠)
$container = new Container();
// 向該 超級工廠添加超人的生產腳本
$container->bind('superman', function ($container, $moduleName) {
    return new Superman($container->make($moduleName));
});
// 向該 超級工廠添加超能力模組的生產腳本
$container->bind('xpower', function ($container) {
    return new XPower();
});
// 同上
$container->bind('ultrabomb', function ($container) {
    return new UltraBomb();
});
// echo '<pre>' . print_r($container, true) . '</pre>';# test
// ****************** 華麗麗的分割線 **********************
// 開始啟動生產
$superman_1 = $container->make('superman', ['xpower']);
// $superman_2 = $container->make('superman', 'ultrabomb');
$superman_3 = $container->make('superman', ['xpower']);
開發者ID:sac071213,項目名稱:dev,代碼行數:31,代碼來源:test.php


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