ntzm/strict-casts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  • stringToBool
  • intToBool
  • stringToInt
  • stringToFloat

And the following general casts, which work with any input, but use strict casts when they can:

  • toBool
  • toInt
  • toFloat

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

GitHub 信息

  • Stars: 20
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固