ntzm/strict-casts
Composer 安装命令:
composer require ntzm/strict-casts
包简介
Strict type casting for super defensive PHP
README 文档
README
Strict type casting for super defensive PHP
Installation
$ composer require ntzm/strict-casts
Usage
This package provides the following strict casts:
stringToBoolintToBoolstringToIntstringToFloat
And the following general casts, which work with any input, but use strict casts when they can:
toBooltoInttoFloat
All mentioned functions live under the namespace StrictCasts. This way, before using them, they have to be imported through the use function statement.
Why?
PHP's inbuilt type casting is not strict at all, and will take almost any type and turn it into another, no matter how valid the conversion is, for example:
(int) 'hello' === 0; (int) '5 hello' === 5; (int) 'hello 5' === 0; (int) '123,456' === 123;
This is a source of a great number of bugs, headaches and crying.
By ensuring that the conversion is valid before conversion, we can ensure that the casting happens exactly as we would expect it to, or it will fail! For example:
use function StrictCasts\stringToInt; stringToInt('123') === 123; stringToInt('-123') === -123; stringToInt('hello'); // throws exception stringToInt('5 hello'); // throws exception stringToInt('hello 5'); // throws exception stringToInt('123,456'); // throws exception
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-20