QuEasy PHP Framework - Types
Classes supporting typed "arrays" to help keep code type-safe. For example, IntArray
, not just array
:
function giveMeInts(IntArray $ints)
{
...
}
Classes allowing to use typed "arrays":
TypedArray
- Base class, implementsArrayAccess
,Iterator
andCountable
IntArray
StringArray
FloatArray
ResourceArray
ObjectArray
ArrayArray
- PHP version 5.3 or higher
composer require v-dem/queasy-type:master-dev
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);
}
}