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


PHP Instance::of方法代碼示例

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


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

示例1: testOf

 public function testOf()
 {
     $container = new Container();
     $className = Component::className();
     $instance = Instance::of($className, $container);
     $this->assertTrue($instance instanceof Instance);
     $this->assertTrue($instance->get() instanceof Component);
     $this->assertTrue(Instance::ensure($instance, $className) instanceof Component);
     $this->assertTrue($instance->get() !== Instance::ensure($instance, $className));
 }
開發者ID:rajanishtimes,項目名稱:basicyii,代碼行數:10,代碼來源:InstanceTest.php

示例2: bootstrap

 /**
  * Bootstrap method to be called during application bootstrap stage.
  * @param Application $app the application currently running
  */
 public function bootstrap($app)
 {
     Yii::$container->set('db', function () {
         return Yii::$app->get('db');
     });
     Yii::$container->set(PackageRepository::class, PackageRepository::class, [Instance::of('db')]);
     Yii::$container->set(StorageInterface::class, function () {
         return Yii::$app->get('packageStorage');
     });
 }
開發者ID:hiqdev,項目名稱:asset-packagist,代碼行數:14,代碼來源:Bootstrap.php

示例3: testDefault

 public function testDefault()
 {
     $namespace = __NAMESPACE__ . '\\stubs';
     $QuxInterface = "{$namespace}\\QuxInterface";
     $Foo = Foo::className();
     $Bar = Bar::className();
     $Qux = Qux::className();
     // automatic wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // full wiring
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set($Bar);
     $container->set($Qux);
     $container->set($Foo);
     $foo = $container->get($Foo);
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure
     $container = new Container();
     $container->set('foo', function () {
         $qux = new Qux();
         $bar = new Bar($qux);
         return new Foo($bar);
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // wiring by closure which uses container
     $container = new Container();
     $container->set($QuxInterface, $Qux);
     $container->set('foo', function (Container $c, $params, $config) {
         return $c->get(Foo::className());
     });
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
     // predefined constructor parameters
     $container = new Container();
     $container->set('foo', $Foo, [Instance::of('bar')]);
     $container->set('bar', $Bar, [Instance::of('qux')]);
     $container->set('qux', $Qux);
     $foo = $container->get('foo');
     $this->assertTrue($foo instanceof $Foo);
     $this->assertTrue($foo->bar instanceof $Bar);
     $this->assertTrue($foo->bar->qux instanceof $Qux);
 }
開發者ID:rajanishtimes,項目名稱:basicyii,代碼行數:55,代碼來源:ContainerTest.php

示例4: getDependencies

 /**
  * Returns the dependencies of the specified class.
  * @param string $class class name, interface name or alias name
  * @return array the dependencies of the specified class.
  */
 protected function getDependencies($class)
 {
     if (isset($this->_reflections[$class])) {
         return [$this->_reflections[$class], $this->_dependencies[$class]];
     }
     $dependencies = [];
     $reflection = new ReflectionClass($class);
     // 獲取類的構造方法
     $constructor = $reflection->getConstructor();
     if ($constructor !== null) {
         // 獲取構造方法的參數,並將其實例化
         foreach ($constructor->getParameters() as $param) {
             if ($param->isDefaultValueAvailable()) {
                 // 如果有defalut值,就直接將默認值放入依賴中
                 $dependencies[] = $param->getDefaultValue();
             } else {
                 // 否則,就先獲取param的ReflectionClass
                 $c = $param->getClass();
                 // 然後通過getName獲取類的名稱(帶namespace),生成一個Instance的實例
                 // Instance中有個id屬性,標記了類的名稱
                 $dependencies[] = Instance::of($c === null ? null : $c->getName());
             }
         }
     }
     // 將類的反射實例和參數依賴的實例分別緩存到_reflections和_dependencies數組中
     $this->_reflections[$class] = $reflection;
     $this->_dependencies[$class] = $dependencies;
     return [$reflection, $dependencies];
 }
開發者ID:glcode,項目名稱:yii2-2.0.3-annotated,代碼行數:34,代碼來源:Container.php

示例5: addErrorFlash

 /**
  *
  */
 public function addErrorFlash()
 {
     /* @var Session $session */
     $session = Instance::of($this->session)->get();
     $session->addFlash('error', $this->errorMessage);
 }
開發者ID:roboapp,項目名稱:base,代碼行數:9,代碼來源:BaseAction.php


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