sj-i/php-cast
Composer 安装命令:
composer require sj-i/php-cast
包简介
Explicit cast with the behavior of implicit weak mode cast
关键字:
README 文档
README
ABOUT
- You can cast any values in the weak mode manner, even if you are in the strict mode (strict_types=1).
INSTALL
composer require sj-i/php-cast
SUPPORTED VERSIONS
- PHP 7.0+
USAGE
use PhpCast\Cast; use PhpCast\NullableCast; // int(1) $int_value = Cast::toInt('1'); // string(1) "1" $string_value = Cast::toString(1); // float(1) $float_value = Cast::toFloat(1); // bool(true) $bool_value = Cast::toBool(1); // TypeError $int_value = Cast::toInt('a'); // TypeError $int_value = Cast::toInt(null); // int(1) $int_value = NullableCast::toInt('1'); // string(1) "1" $string_value = NullableCast::toString(1); // float(1) $float_value = NullableCast::toFloat(1); // bool(true) $bool_value = NullableCast::toBool(1); // TypeError $null_value = NullableCast::toInt(''); // null $null_value = NullableCast::toInt(null);
HOW IT WORKS
- Return type declarations in weak mode files do the job.
PhpCast\Castis defined in a file declared asstrict_types=0- Though
strict_types=0is the current default of PHP, it is explicitly declared to assert the intent.
- Though
- The type checks for parameters are done in the caller mode, but the type checks for return values are always done in the callee mode.
"WHY WEAK MODE? I WANT TO USE STRICT MODE IN EVERYWHERE!"
- To use
strict_types=1without anystrict_types=0codes, explicit casts have to be used for untyped data from external sources like DB or HTTP requests. - But in PHP, explicit cast like
(int)$foonever fails with a few exceptions.(int)'abc'silently results to0and doesn't emit a warning.
- So some validations are necessary before just using casts.
- If explicit casts are used without proper validations by a lazy programmer, the purpose of the type declarations are totally ruined.
- This was, if I understand it correctly, one of the reasons that strict type checks weren't totally welcomed with open arms during the discussion introducing the scalar type hinting to PHP.
- Defining proper rules of validations and type conversions is sometimes overkill.
- Even though defining them are always "correct" things to do, in humans life, there are times you don't need to be so correct.
- Let's cast untyped values in the weak mode manner, use the "official" rules of validations and type conversions in the PHP world!
- No need to invent and learn new rules every day every night everywhere.
- If the "official" rules doesn't fit your needs, then define and use your own rules selectively there.
- If you've ever read the accepted RFC of STH, you would notice the use of weak mode like this library has been already mentioned there.
This proposal is not a compromise. It is an attempt of allowing strict typing to work in PHP. A mechanism to bridge untyped PHP code with strict typed PHP code, a “weak” bridge, would be required (otherwise explicit (type) casts would be needed). This proposal unifies the strict and weak typing into a single system that integrates tightly and behaves consistently.
LICENSE
MIT
sj-i/php-cast 适用场景与选型建议
sj-i/php-cast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 446.69k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sj-i/php-cast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sj-i/php-cast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sj-i/php-cast 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convenient array-related routine & better type casting
A package to cast json fields, each sub-keys is castable
PhpPlaisio (TypeScript): Safely cast mixed values to bool, number (float or integer), or string
Validation rules and an Eloquent Model attribute cast to value object encapsulating a South African government-issued personal identification number, for Laravel.
A database with ISO country information in all configured translations
统计信息
- 总下载量: 446.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 22
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-06