francalek / datatype
PHP 使用的有用数据类型。
v0.1
2022-07-06 15:17 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^9
README
要求
此包与 PHP >= 7.1 兼容
安装
安装此包的最佳方法是使用 Composer
$ composer require francalek/datatype
使用方法
CountableIterator
此抽象类仅实现了 \Countable 和 \Iterator 接口,并有自己的存储。
迭代和计数的必须数据应放在受保护的属性 $this->countableIterator
中。
use Francalek\DataType\CountableIterator; class Sheeps extends CountableIterator { public function __construct(int $sheeps = 5) { $herd = array_fill(0, $sheeps, 'Sheep'); $this->countableIterator = $herd; } }
现在您可以迭代您的类
echo "I can't sleep :(\n"; $sheeps = new Sheeps(); foreach ($sheeps as $nr => $sheep) { echo $sheep.' '.(++$nr)."\n"; } exit("Chrrr...\n");
或者计数
echo "I can't sleep :(\n"; $sheeps = new Sheeps(); echo "I have ".count($sheeps)." sheeps in total.\n"; exit("Chrrr...\n");
运行测试
要运行测试
$ git clone https://github.com/francalek/DataType.git $ cd DataType/ $ ./tests/run.sh # Or over Composer $ composer tests
贡献
许可协议
此包受 MIT 许可协议 的许可。