v-dem / queasy-type
Type helper classes, part of QuEasy PHP framework
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-11-29 06:29:29 UTC
README
QuEasy PHP Framework - Types
Package v-dem/queasy-type
Classes supporting typed "arrays" to help keep code type-safe. For example, IntArray
, not just array
:
function giveMeInts(IntArray $ints) { ... }
Features
Classes allowing to use typed "arrays":
TypedArray
- Base class, implementsArrayAccess
,Iterator
andCountable
IntArray
StringArray
FloatArray
ResourceArray
ObjectArray
ArrayArray
Requirements
- PHP version 5.3 or higher
Installation
composer require v-dem/queasy-type:master-dev
Usage
For example, to create an array of integer values:
$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
To create a specialized class representing array of users:
class UsersArray extends TypedArray { public function __construct(array $items = null) { parent::__construct(app\model\User::class, $items); } }