• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP Closure类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: getServiceInstance

 /**
  * Get service instance
  *
  * @param ServiceLocatorInterface $serviceLocator Service Locator instance
  * @return mixed
  */
 public function getServiceInstance(ServiceLocatorInterface $serviceLocator)
 {
     if ($this->instance === null) {
         /**
          * Instance must be created using factory
          */
         if ($this->factory instanceof \Closure) {
             $instance = call_user_func($this->factory, $serviceLocator);
         } else {
             $instance = $this->factory->factory($serviceLocator);
         }
         if (!$this->isShared()) {
             return $instance;
             // new instance must be returned
         } else {
             $this->instance = $instance;
             // cache instance
             return $this->instance;
             // same instance must be returned
         }
     } else {
         /**
          * Instance either was passed directly or has been created earlier
          */
         if (!$this->isShared()) {
             return $this->instance;
             // new instance must be returned
         } else {
             return clone $this->instance;
             // same instance must be returned
         }
     }
 }
开发者ID:roman-kulish,项目名称:newscorp-servicelocator,代码行数:39,代码来源:ServiceLocatorService.php


示例2: testCorrect

 /**
  * @param string $type
  * @param \Closure $generateImage
  * @param \Closure $checkOutput
  * @param array $logLevels
  * @dataProvider imageProvider
  */
 public function testCorrect(string $type, \Closure $generateImage, \Closure $checkOutput, array $logLevels = [])
 {
     $validator = new ImageValidator($type, '');
     $validator->setLogger($this);
     $checkOutput->call($this, $validator->correct($generateImage()));
     $this->assertEquals($logLevels, $this->logLevels);
 }
开发者ID:esperecyan,项目名称:dictionary-php,代码行数:14,代码来源:ImageValidatorTest.php


示例3: on

 public function on($event, \Closure $callback)
 {
     if (is_string($event)) {
         $this->events[$event] = $callback->bindTo($this, $this);
     }
     return $this;
 }
开发者ID:cyan-framework,项目名称:library,代码行数:7,代码来源:websocket.php


示例4: proceed

 /**
  * Invokes original method and return result from it
  *
  * @return mixed
  */
 public function proceed()
 {
     if (isset($this->advices[$this->current])) {
         /** @var $currentInterceptor \Go\Aop\Intercept\Interceptor */
         $currentInterceptor = $this->advices[$this->current++];
         return $currentInterceptor->invoke($this);
     }
     // Fill the closure only once if it's empty
     if (!$this->closureToCall) {
         $this->closureToCall = $this->reflectionMethod->getClosure($this->instance);
     }
     // Rebind the closure if instance was changed since last time
     if ($this->previousInstance !== $this->instance) {
         $this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class);
         $this->previousInstance = $this->instance;
     }
     $closureToCall = $this->closureToCall;
     $args = $this->arguments;
     switch (count($args)) {
         case 0:
             return $closureToCall();
         case 1:
             return $closureToCall($args[0]);
         case 2:
             return $closureToCall($args[0], $args[1]);
         case 3:
             return $closureToCall($args[0], $args[1], $args[2]);
         case 4:
             return $closureToCall($args[0], $args[1], $args[2], $args[3]);
         case 5:
             return $closureToCall($args[0], $args[1], $args[2], $args[3], $args[4]);
         default:
             return forward_static_call_array($closureToCall, $args);
     }
 }
开发者ID:williamamed,项目名称:Raptor2,代码行数:40,代码来源:ClosureDynamicProceedTrait.php


示例5: get

 public function get()
 {
     if (!$this->closure instanceof \Closure) {
         throw new \RuntimeException("Cannot get a key with an undefined closure");
     }
     if ($this->enabled) {
         $data = $this->predis->get($this->getKeyName($this->key));
     }
     if ($data === null) {
         $boundCallback = $this->closure->bindTo($this);
         $data = $boundCallback();
         if ($this->enabled) {
             $this->predis->setex($this->getKeyName($this->key), $this->ttl, serialize($data));
         }
         if ($this->onMiss instanceof \Closure) {
             call_user_func_array($this->onMiss, [$this, $data]);
         }
         return $data;
     } else {
         $value = unserialize($data);
         if ($this->onHit instanceof \Closure) {
             call_user_func_array($this->onHit, [$this, $value]);
         }
         return $value;
     }
 }
开发者ID:jimbojsb,项目名称:cacheback,代码行数:26,代码来源:Key.php


示例6: __construct

 /**
  * Constructs a Spec, to be associated with a particular suite, and ran
  * by the test runner. The closure is bound to the suite.
  *
  * @param string   $title   A title to be associated with the spec
  * @param \Closure $closure The closure to invoke when the spec is called
  * @param Suite    $suite   The suite within which this spec was defined
  */
 public function __construct($title, \Closure $closure = null, Suite $suite)
 {
     $this->title = $title;
     $this->suite = $suite;
     if ($closure) {
         $this->closure = $closure->bindTo($suite);
     }
 }
开发者ID:gsouf,项目名称:pho,代码行数:16,代码来源:Spec.php


示例7: myCapture

function myCapture(Closure $closure)
{
    ob_start();
    $closure->__invoke();
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}
开发者ID:serendip811,项目名称:patio42,代码行数:8,代码来源:phpunit.php


示例8: fingerprint

 public function fingerprint(\Closure $fingerprint = null)
 {
     if (func_num_args() > 0) {
         $this->_fingerprint = $fingerprint->bindTo($this);
         return $this;
     }
     return $this->_fingerprint;
 }
开发者ID:jacksleight,项目名称:coast,代码行数:8,代码来源:Session.php


示例9: add

 /**
  * Registers a route.
  *
  * @param  string $name
  * @param  \Closure $handler
  *
  * @return Route
  */
 public function add($name, \Closure $handler)
 {
     // Bind the handler to the Router class
     $route = new Route($name, $handler->bindTo($this, $this));
     // Insert the route
     $this->routes[$name] = $route;
     return $route;
 }
开发者ID:domynation,项目名称:domynation-framework,代码行数:16,代码来源:Router.php


示例10: group

 /**
  * @param string $prefix
  * @param \Closure $closure
  */
 public function group($prefix, \Closure $closure)
 {
     $original = $this->prefix;
     $this->prefix = sprintf('/%s/%s', trim($original, '/'), trim($prefix, '/'));
     $callback = $closure->bindTo($this);
     $callback();
     $this->prefix = $original;
 }
开发者ID:pkdevboxy,项目名称:tez,代码行数:12,代码来源:Router.php


示例11: __construct

 public function __construct(\Closure $closure, Suite $suite)
 {
     $this->suite = $suite;
     if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
         $this->closure = $closure->bindTo($suite);
     } else {
         $this->closure = $closure;
     }
 }
开发者ID:ciarand,项目名称:pho,代码行数:9,代码来源:RunnableSpec.php


示例12: fetch

 /**
  * Retrieves data by key or executes $callback in case of cache miss. Data returned
  * by $callback is then cached.
  *
  *     \rox\Cache::fetch('my_key', '+1 hour', function(){
  *         // some expensive operation
  *         return $results;
  *     });
  *
  * @param string $key 
  * @param string $expires expiration time in seconds or strtotime() compatible string
  * @param \Closure $callback closure to be executed on cache miss
  * @return mixed
  */
 public static function fetch($key, $expires, \Closure $callback)
 {
     $data = static::read($key);
     if ($data === false) {
         $data = $callback->__invoke();
         static::write($key, $data, $expires);
     }
     return $data;
 }
开发者ID:raymondjavaxx,项目名称:roxphp,代码行数:23,代码来源:Cache.php


示例13: runLegacyKernelCallback

 /**
  * Executes a legacy kernel callback
  *
  * Does the callback with both post-reinitialize and formtoken checks disabled.
  *
  * @param callable $callback
  *
  * @return mixed
  */
 protected function runLegacyKernelCallback($callback)
 {
     // Initialize legacy kernel if not already done
     if ($this->legacyKernel instanceof Closure) {
         $legacyKernelClosure = $this->legacyKernel;
         $this->legacyKernel = $legacyKernelClosure();
     }
     return $this->legacyKernel->runCallback($callback, false, false);
 }
开发者ID:CG77,项目名称:ezpublish-kernel,代码行数:18,代码来源:AbstractLegacySlot.php


示例14: mockFindUserResponse

 /**
  * Build where clause response.
  *
  * @param  Closure|bool  $expectation
  * @return mixed
  */
 private function mockFindUserResponse($expectation)
 {
     if (!$expectation instanceof Closure) {
         return $expectation;
     }
     $user = Mockery::mock(User::class);
     // Run the expectations for the returned user mock.
     $expectation->__invoke($user);
     return $user;
 }
开发者ID:salpakan,项目名称:salpakan,代码行数:16,代码来源:ValidatesUserTest.php


示例15: connect

 /**
  * Attach a handler to the session validator chain
  * 
  * @param  string $topic 
  * @param  string|object|Closure $context 
  * @param  null|string $handler 
  * @return Zend\Stdlib\SignalHandler
  */
 public function connect($topic, $context, $handler = null)
 {
     if ($context instanceof Validator) {
         $data = $context->getData();
         $name = $context->getName();
         $this->getStorage()->setMetadata('_VALID', array($name => $data));
     }
     $handle = parent::connect($topic, $context, $handler);
     return $handle;
 }
开发者ID:bradley-holt,项目名称:zf2,代码行数:18,代码来源:ValidatorChain.php


示例16: testRepoIsPassedToTheClosureAsFirstParameter

 public function testRepoIsPassedToTheClosureAsFirstParameter()
 {
     $repo = $this->getRepo('foobar');
     $self = $this;
     $criterion = new Closure(function ($param) use($repo, $self) {
         $self->assertEquals($repo, $param);
         return $param === $repo;
     });
     $this->assertTrue($criterion->matches($repo));
 }
开发者ID:ruian,项目名称:KnpBundles,代码行数:10,代码来源:ClosureTest.php


示例17: __construct

 /**
  * Constructs a test suite, which may contain nested suites and specs. The
  * anonymous function passed to the constructor contains the body of the
  * suite to be ran, and it is bound to the suite.
  *
  * @param string   $title   A title to be associated with the suite
  * @param \Closure $closure The closure to invoke when the suite is ran
  * @param Suite    $parent  An optional parent suite
  */
 public function __construct($title, \Closure $closure, Suite $parent = null)
 {
     $this->title = $title;
     $this->closure = $closure->bindTo($this);
     $this->specs = [];
     $this->suites = [];
     $this->store = [];
     $this->parent = $parent;
     $this->pending = false;
 }
开发者ID:gsouf,项目名称:pho,代码行数:19,代码来源:Suite.php


示例18: __construct

 /**
  * Constructs a Spec, to be associated with a particular suite, and ran
  * by the test runner. The closure is bound to the suite.
  *
  * @param string   $title   A title to be associated with the spec
  * @param \Closure $closure The closure to invoke when the spec is called
  * @param Suite    $suite   The suite within which this spec was defined
  */
 public function __construct($title, \Closure $closure = null, Suite $suite)
 {
     $this->title = $title;
     $this->suite = $suite;
     if ($closure && version_compare(PHP_VERSION, '5.4.0', '>=')) {
         $this->closure = $closure->bindTo($suite);
     } else {
         if ($closure) {
             $this->closure = $closure;
         }
     }
 }
开发者ID:ciarand,项目名称:pho,代码行数:20,代码来源:Spec.php


示例19: __invoke

 /**
  * {@inheritdoc}
  */
 public function __invoke($input, $index)
 {
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     /** @noinspection PhpVoidFunctionResultUsedInspection */
     return $this->function->__invoke($input, $index);
 }
开发者ID:peekandpoke,项目名称:psi,代码行数:10,代码来源:BinaryClosure.php


示例20: entityManager

 public static final function entityManager()
 {
     if (self::$entityManager === null) {
         self::$entityManager = self::$entityManagerFactory->__invoke();
     }
     return self::$entityManager;
 }
开发者ID:elfchat,项目名称:elfchat,代码行数:7,代码来源:Entity.php



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP Cloud类代码示例发布时间:2022-05-23
下一篇:
PHP Checker类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap