承接 jgswift/delegatr 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jgswift/delegatr

Composer 安装命令:

composer require jgswift/delegatr

包简介

PHP 5.5+ delegate system

README 文档

README

PHP 5.5+ delegate system

Build Status Scrutinizer Code Quality Latest Stable Version License Coverage Status

Installation

Install via cli using composer:

php composer.phar require jgswift/delegatr:0.1.*

Install via composer.json using composer:

{
    "require": {
        "jgswift/delegatr": "0.1.*"
    }
}

Dependency

Usage

Built-in Lambda (with Serializable)

One of the cumbersome aspects of closures in php is the need to explicitly define the context variables with the "use" statement. Delegates can make that process less painful by allowing you to specify an associative array to dynamically define context.

$x = 10;
$y = 2;

$lambda = new delegatr\Lambda(function() {
    return $x + $y;
}, get_defined_vars());

var_dump($lambda()); // 12

get_defined_vars returns a (non-referenced) list of variables defined in the same scope get_defined_vars is called. Thus $x and $y are dynamically added the the closure's "use" statement and inserted into the closure scope at run-time.

Writing Custom Serializable Closures (using trait)

Delegatr doesn't just wrap closures in an object, it uses eval to enable the serialization of closures - something native php can not do.

The following class uses the Serializable delegate to accomplish this.

class MyDelegate {
    use delegatr\Serializable;
}

$delegate = new MyDelegate(function() {
    return 'foo';
});

$serial_string = serialize($delegate);

$delegate2 = unserialize($serial_string)

var_dump($delegate2()); // returns 'foo';

Using the above trait prevents delegatr from imposing on your domain but if that isn't a concern just instantiate or inherit the Lambda class as shown below.

$delegate = new delegatr\Lambda(function() {
    return 'foo';
});

class MyLambda extends delegatr\Lambda {
    /* ... */
}

Simple delegate (without serialization)

A simpler implementation is also available without \Serializable included

class MyDelegate {
    use delegatr\Delegate;
}

$delegate = new MyDelegate(function() {
    return 'foo';
});

var_dump($delegate()); // returns 'foo';

Eval Fallback & Vulnerability

The eval function is heavily relied on in this package.
If you are not comfortable with eval or do not understand the security risks, I do not suggest you use this package.

However, eval itself is not required for this package.
Delegatr uses adlawson/veval.php to compile scripts at run-time even in environments where eval is disabled. Bypassing eval in this way doesn't reduce the risk of code injection.

统计信息

  • 总下载量: 161
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固