peridot-php/peridot-scope

用于函数绑定和混入的Scopes

1.3.0 2016-02-22 13:27 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:52:53 UTC


README

Build Status HHVM Status

Peridot Scope.

Scopes 允许在闭包中安全绑定状态,并通过 子Scope 提供混合状态和行为的方法。

Peridot 测试框架提取。

##使用方法

我们建议通过 composer 安装此包

$ composer require peridot-php/peridot-scope:~1.0

###创建Scope

$scope = new Scope();
$scope->name = "Brian";

$fnWithName = function() {
    print $this->name;
};

$fnWithName = $scope->peridotBindTo($fnWithName);

$fnWithName(); //prints "Brian"

###使用ScopeTrait

如果现有类可以从Scope中受益,您可以使用 ScopeTrait

class Test
{
    use ScopeTrait;
    
    protected $definition;
    
    public function __construct(callable $definition)
    {
        $this->definition = $definition; 
    }
    
    /**
     * Return the definition bound to a scope
     */
    public function getDefinition()
    {
        $scope = $this->getScope();
        return $scope->peridotBindTo($this->definition);
    }
}

##混入

您可以通过 子Scope 混入行为。