当前位置: 首页>>代码示例>>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;未经允许,请勿转载。