granam / strict-scalar
2.1.2
2015-09-16 11:22 UTC
Requires
- php: >=5.4
- granam/exception-hierarchy: ~2.1
- granam/scalar: ~1.0
- granam/strict-object: ~1.1
Requires (Dev)
- mockery/mockery: >=0.9
- phpunit/phpunit: ~4.4
README
PHP尚未提供标量类型提示(计划在PHP 7.0中实现)。
因此,如果我们想确保标量类型,类型检查类是唯一的机会。
警告:非严格标量不会将null转换,它将保持为null。
<?php
use Granam\Strict\Scalar\StrictScalar;
use Granam\Strict\Scalar\Exceptions\WrongParameterType;
$scalar = new StrictScalar('foo');
// foo
echo $scalar;
$nullScalar = new StrictScalar(null, false /* suppressed strictness */);
// false
echo is_scalar($nullScalar->getValue());
// true
echo is_null($nullScalar->getValue());
try {
new StrictScalar(null);
} catch (WrongParameterType $scalarException) {
// Strict scalar has to get a scalar value. Null is not a scalar.
die('Something get wrong: ' . $scalarException->getMessage());
}