donurks / php-typed-array
类型数组的基类。
1.0.0
2017-08-19 23:46 UTC
Requires (Dev)
- phpunit/phpunit: 6.3.0
This package is not auto-updated.
Last update: 2024-09-29 04:42:49 UTC
README
php-typed-array
一个用于类型安全数组的PHP类。该项目基于PHP的ArrayObject。
特性
- 类型安全数组
- PHP语言类型的数组
- 布尔型
- 整型
- 字符串型
- 浮点型
安装(使用Composer)
命令行
composer require donurks/php-typed-array
使用方法
类
<?php chdir(dirname(__DIR__)); require_once "vendor/autoload.php"; class MyOwnType extends \Donurks\AbstractTypedArray { protected $type = \stdClass::class; } $myOwnType = new MyOwnType([ new \stdClass(), new \stdClass(), new \stdClass(), ]);
PHP语言类型
<?php chdir(dirname(__DIR__)); require_once "vendor/autoload.php"; $strings = new \Donurks\TypedArray\TypeString([ 'string1', 'string2', 'string3' ]); $booleans = new \Donurks\TypedArray\TypeBoolean([ true, false, true ]); $integers = new \Donurks\TypedArray\TypeInteger([ 1, 124, 3434 ]); $floats = new \Donurks\TypedArray\TypeFloat([ 1.234, 1.2e3, 7E-10 ]);
异常
<?php chdir(dirname(__DIR__)); require_once "vendor/autoload.php"; $strings = new \Donurks\TypedArray\TypeString([ 'string1', 'string2', 'string3' ]); $booleans = new \Donurks\TypedArray\TypeBoolean([]); $booleans[] = true; try { $booleans[] = 'not-a-boolean'; } catch (\Donurks\TypedArray\Exception $e) { die($e->getMessage()); }