v-dem / queasy-type
类型辅助类,QuEasy PHP 框架的一部分
dev-main
2022-12-30 18:09 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- php: >=7.1.0
- ext-xdebug: *
- phpunit/phpunit: ~7
This package is auto-updated.
Last update: 2024-08-29 05:36:31 UTC
README
QuEasy PHP 框架 - 类型
包 v-dem/queasy-type
支持类型化 "数组" 的类,有助于保持代码类型安全。例如,IntArray
,不仅仅是 array
function giveMeInts(IntArray $ints) { ... }
特性
允许使用类型化 "数组" 的类
TypedArray
- 基础类,实现了ArrayAccess
,Iterator
和Countable
IntArray
StringArray
FloatArray
ResourceArray
ObjectArray
ArrayArray
要求
- PHP 版本 5.3 或更高
安装
composer require v-dem/queasy-type:master-dev
使用
例如,创建一个整数数组
$intArray = new queasy\type\IntArray([10, 20, 30]); $intArray[] = 40; $intArray['key'] = 50; unset($intArray[0]); // Will remove value 10 foreach ($intArray as $key => $value) { echo $key . ' => ' . $value . PHP_EOL; } echo 'Elements count: ' . count($intArray) . PHP_EOL; $intArray[] = 'wrong'; // Throws InvalidArgumentException
创建一个表示用户数组的专用类
class UsersArray extends TypedArray { public function __construct(array $items = null) { parent::__construct(app\model\User::class, $items); } }