alcamo/decorator
最新稳定版本:0.1.1
Composer 安装命令:
composer require alcamo/decorator
包简介
Base trait and class to create decorators
关键字:
README 文档
README
use alcamo\decorator\Decorator;
use Ds\Set;
class SetWithSum extends Decorator implements \Countable, \IteratorAggregate
{
public function __construct($values)
{
parent::__construct(new Set($values));
}
public function getSum(): int
{
$result = 0;
foreach ($this as $value) {
$result += $value;
}
return $result;
}
}
$set = new SetWithSum([ 42, 42, 7]);
echo "Count: " . count($set) . "\n";
echo "Sum: " . $set->getSum() . "\n";
This will output:
Count: 2
Sum: 49
Overview
The trait DecoratorTrait implements the Decorator
pattern for an
arbitrary object. The class Decorator uses this trait.
As in the example above, this can be used to create a class that adds functionality to a class when creating a derived class is not possible.
All functionality of the enclosed object is exposed by the decorator so that the decorator behaves just as the enclosed object, supporting, for instance, the Countable, Iterator or ArrayAccess interface.
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2025-10-22