granam / strict-float
2.1.1
2015-09-10 11:51 UTC
Requires
- php: >=5.4
- granam/exception-hierarchy: ~2.1
- granam/float: ~1.0
- granam/strict-scalar: ~2.0
Requires (Dev)
- mockery/mockery: >=0.9
- phpunit/phpunit: ~4.4
README
PHP尚未提供标量类型提示(计划在PHP 7.0中提供)(尚不支持)。
因此,如果我们想要确保标量类型,类型检查类是唯一的机会。
注意:需要PHP 5.4以上版本
<?php use Granam\Strict\Float\StrictFloat; use Granam\Strict\Float\Exceptions\WrongParameterType; $float = new StrictFloat(123.456); // double(123.456) var_dump($float->getValue()); // string(7) "123.456" var_dump((string)$float); $float = new StrictFloat(null, false /* explicitly non-strict*/); // double(0) var_dump($float->getValue()); // string(0) var_dump((string)$float); try { new StrictFloat(null); } catch (WrongParameterType $floatException) { // Something get wrong: On strict mode expected float only, got NULL die('Something get wrong: ' . $floatException->getMessage()); }