摩迪利昂 / mutils
有用的辅助工具
v1.2.3
2023-09-13 15:38 UTC
Requires
- php: >=7.1 || >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.20
README
MUtils 是一个有用的函数集合,并提供一些 PHP 8.0+ 的特性给旧版本的 PHP。
PHP 8.0+ 特性
如果你使用这个库中的函数,它会检查你是否使用 PHP 8.0+,如果是,则使用内部函数(直接通过)。
- 排序函数(如果不直接通过,则性能较慢! - https://wiki.php.net/rfc/stable_sorting)
- asort, arsort, ksort, krsort, natcasesort, natsort, rsort, sort, uasort, uksort, usort
- 字符串函数
- str_contains, str_ends_with, str_starts_with
有用函数
- 数组函数 - 优化与数组工作的函数
- array_compare_flags, array_move_element, array_group_by, array_prefix_add, array_prefix_remove
- 位操作函数 - 一些用于位集合的有用函数
- bit_set, bit_isset, bit_unset
- 反射函数 - 获取继承的 ReflectionClass、ReflectionMethod 和 ReflectionProperty 的有用函数
- get_reflection_class, get_reflection_method, get_reflection_property
示例
数组函数的使用
<?php $array = [128, 2, 16, 64, 8, 1, 32]; // or MUtils\Arrays::userAssociativeSort() MUtils\Arrays\uasort($array, static function ($left, $right) { return $left <=> $right; })
字符串函数的使用
<?php // or MUtils\Strings::startsWith() if (MUtils\Strings\str_starts_with('Some Text', 'Some')) { // ... }
位函数的使用
<?php // or MUtils\Bits::isset() if (MUtils\Bits\bit_isset(12, 4)) { // ... }
反射函数的使用
<?php // or MUtils\Objects::getReflectionMethod() $instance = new Something(); $method = MUtils\Objects\get_reflection_method($instance, 'foo'); if ($method) { $method->invoke($instance, 'arg'); // ... }