ahmedzidan/php-value
Composer 安装命令:
composer require ahmedzidan/php-value
包简介
A modern replacement for PHP’s gettype function.
README 文档
README
Value
Modern replacement for PHP’s gettype function.
Installation
Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:
$ composer require fleshgrinder/value
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Usage
The Value class currently contains a single static method called getValue
and constants with the names of each of PHP’s types, corresponding to the
values returned by the getValue method. There are currently no plans to
extend this API and the following functionality is considered feature complete
unless new types are added or old ones are removed.
<?php use Fleshgrinder\Core\Value; $closed_resource = tmpfile(); fclose($closed_resource); $values = [ [], true, 1.0, 1, null, new stdClass, new DateTimeImmutable, tmpfile(), $closed_resource, 'string' ]; foreach ($values as $value) { echo Value::getType($value) , "\n"; }
The above will output the following:
array
boolean
float
integer
null
stdClass
DateTimeImmutable
string
resource
closed resource
This output corresponds to the available constants in the class with the exception for the concrete class names and the additionally available callable and iterable pseudo type names:
<?php use Fleshgrinder\Core\Value; echo Value::TYPE_ARRAY; // array echo Value::TYPE_BOOL; // boolean echo Value::TYPE_CALLABLE; // callable echo Value::TYPE_CLOSED_RESOURCE; // closed resource echo Value::TYPE_FLOAT; // float echo Value::TYPE_INT; // integer echo Value::TYPE_ITERABLE; // iterable echo Value::TYPE_NULL; // null echo Value::TYPE_OBJECT; // object echo Value::TYPE_RESOURCE; // resource echo Value::TYPE_STRING; // string
The method comes in very handy during the creation of error messages. One of the most repeated patterns in the PHP world is the following:
$type = is_object($arg) ? get_class($arg) : gettype($arg);
Which is not only cumbersome but also produces inconsistent type names due to
PHP’s inconsistent gettype function. This can be replaced with the method
provided by this library.
$type = Value::getType($arg);
The method never throws anything and does not emit errors. The sole goal of this tiny library is to keep code DRY as much as possible, after all, it is one of the most important principle of them all.
I tried to get this change into PHP core but it was rejected, see
PHP RFC: var_type and the linked
resources at the bottom for more details.
ahmedzidan/php-value 适用场景与选型建议
ahmedzidan/php-value 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ahmedzidan/php-value 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ahmedzidan/php-value 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unlicense
- 更新时间: 2022-03-20