當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。