当前位置: 首页>>代码示例>>PHP>>正文


PHP Container::singleton方法代码示例

本文整理汇总了PHP中Container::singleton方法的典型用法代码示例。如果您正苦于以下问题:PHP Container::singleton方法的具体用法?PHP Container::singleton怎么用?PHP Container::singleton使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Container的用法示例。


在下文中一共展示了Container::singleton方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testSingletonWithPersistence

 public function testSingletonWithPersistence()
 {
     $container = new Container();
     $container->singleton('mySingleton', '\\stdClass');
     $stdClass = $container->build('mySingleton');
     $stdClass->testVar = 'value';
     $stdClass2 = $container->build('mySingleton');
     $this->assertEquals('value', $stdClass2->testVar);
 }
开发者ID:usmanhalalit,项目名称:viocon,代码行数:9,代码来源:ContainerTest.php

示例2: get

 /**
  * Convert a callback expression to a valid PHP callback.
  *
  * @param string|array $id
  *   A callback expression; any of the following.
  *
  * @return array
  *   A PHP callback. Do not serialize (b/c it may include an object).
  * @throws \RuntimeException
  */
 public function get($id)
 {
     if (!is_string($id)) {
         // An array or object does not need to be further resolved.
         return $id;
     }
     if (strpos($id, '::') !== FALSE) {
         // Callback: Static method.
         return explode('::', $id);
     } elseif (strpos($id, '://') !== FALSE) {
         $url = parse_url($id);
         switch ($url['scheme']) {
             case 'obj':
                 // Object: Lookup in container.
                 return Container::singleton()->get($url['host']);
             case 'call':
                 // Callback: Object/method in container.
                 $obj = Container::singleton()->get($url['host']);
                 return array($obj, ltrim($url['path'], '/'));
             case 'api3':
                 // Callback: API.
                 return new ResolverApi($url);
             case 'global':
                 // Lookup in a global variable.
                 return new ResolverGlobalCallback($url['query'], $url['host'] . (isset($url['path']) ? rtrim($url['path'], '/') : ''));
             default:
                 throw new \RuntimeException("Unsupported callback scheme: " . $url['scheme']);
         }
     } elseif (in_array($id, array('0', '1'))) {
         // Callback: Constant value.
         return new ResolverConstantCallback((int) $id);
     } elseif ($id[0] >= 'A' && $id[0] <= 'Z') {
         // Object: New/default instance.
         return new $id();
     } else {
         // Callback: Function.
         return $id;
     }
 }
开发者ID:kidaa30,项目名称:yes,代码行数:49,代码来源:Resolver.php

示例3: testObj

 /**
  * Test object-lookup in the container.
  */
 public function testObj()
 {
     // Note: ResolverTestExampleService is implemented at the bottom of this file.
     Container::singleton()->set('callbackTestService', new ResolverTestExampleService());
     $obj = $this->resolver->get('obj://callbackTestService');
     $this->assertTrue($obj instanceof ResolverTestExampleService);
 }
开发者ID:rajeshrhino,项目名称:civicrm-core,代码行数:10,代码来源:ResolverTest.php

示例4: registerAccessHandler

 protected function registerAccessHandler()
 {
     $this->container->singleton('access', function ($app) {
         return $app->build(AccessHandler::class);
     });
 }
开发者ID:alfredoem,项目名称:container,代码行数:6,代码来源:Application.php


注:本文中的Container::singleton方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。