ousaa / utils
Some commonly used functions and classes
v3.0.1
2026-10-22 10:18 UTC
Requires
- php: >=8.2
- ext-mbstring: *
- ext-pcntl: *
- ext-posix: *
- ext-redis: *
- phpmailer/phpmailer: ^6.9
- symfony/cache: ^5.4
- symfony/console: >=5.4
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-22 10:46:10 UTC
README
常用的函数和类打包
工具类
工具方法都是 Ousaa\Utils\Helpers 下的静态类,类名统一以 Util 开头,和项目或框架里的同名类区分开;输出相关的方法在 Ousaa\Utils\Library\Output\SimpleOutput。包里不定义任何函数。
use Ousaa\Utils\Helpers\UtilDate;
use Ousaa\Utils\Helpers\UtilStr;
use Ousaa\Utils\Library\Output\SimpleOutput;
SimpleOutput::info('开始:' . UtilDate::datetime(), ['k' => 'v']); // 可以传多个,非字符串直接打印结构
$field = UtilStr::snake('createTime'); // create_time
| 类 | 方法 |
|---|---|
SimpleOutput | info error infoAndExit errorAndExit table log,以及 comment warning highlight question |
UtilDump | dump dumpAndExit cliDump cliDumpAndExit memory |
UtilDate | datetime date format byTimezone strtotimeByTimezone cn strtotimeCn timezoneOffset microTime |
UtilRandom | hex alnum digits letters upperLetters |
UtilStr | camel snake(规则与 ThinkPHP 的 think\helper\Str 一致) |
UtilArr | toMap group randomElements |
UtilCsv | read write |
UtilNumber | floatLimit |
UtilLog | write |
UtilRedis | create |
项目里常用的几个方法如果想保留短的全局写法,在项目自己的 common.php 里定义一行转调用即可(下面的迁移脚本会自动生成),不要套 function_exists:
function output_info(...$messages)
{
SimpleOutput::info(...$messages);
}
从 v2 升级到 v3
v2 的辅助函数定义在全局命名空间,会和引用方或其他包的同名函数冲突(直接报 Cannot redeclare,或者被 function_exists 静默跳过、换成另一套实现),v3 全部改为静态类。
composer.json里把ousaa/utils改成 v3 并更新依赖在项目根目录执行迁移脚本:
php vendor/ousaa/utils/bin/migrate-to-v3.php --dry-run app tests config route database # 先预览 php vendor/ousaa/utils/bin/migrate-to-v3.php app tests config route database # 再执行- 调用改写成静态方法并补上
use导入,如camel_case($s)改为UtilStr::camel($s) output_infooutput_erroroutput_info_exitoutput_error_exitto_datetimedate_fmtget_all_unique_id这 7 个保留全局写法,调用处不动,在app/common.php(或用--common=<文件>指定)末尾生成转调用的定义- 项目自己定义了同名函数时(如老项目
common.php里的副本),它的调用不改写
- 调用改写成静态方法并补上
处理脚本输出里以
!开头的提示:字符串形式引用的函数名(如call_user_func('camel_case'))、项目自定义的同名函数、调用了已删除的函数
函数与方法的对应关系:
| v2 函数 | v3 方法 |
|---|---|
output_info output_error output_info_exit output_error_exit | SimpleOutput::info error infoAndExit errorAndExit |
output_table custom_output_log | SimpleOutput::table log |
ddp ddd dpl ddl dump_memory | UtilDump::dump dumpAndExit cliDump cliDumpAndExit memory |
to_datetime to_date date_fmt | UtilDate::datetime date format |
date_by_timezone strtotime_by_timezone get_timezone_number | UtilDate::byTimezone strtotimeByTimezone timezoneOffset |
date_cn_time strtotime_cn date_micro_time | UtilDate::cn strtotimeCn microTime |
get_unique_id get_all_unique_id get_unique_num | UtilRandom::hex alnum digits |
get_rand_letlter get_rand_letlter_capital | UtilRandom::letters upperLetters |
camel_case snake_case | UtilStr::camel snake |
array_to_map array_to_matrix random_elements | UtilArr::toMap group randomElements |
csv_to_array array_to_csv | UtilCsv::read write |
float_limit custom_log get_simple_redis_ins | UtilNumber::floatLimit UtilLog::write UtilRedis::create |
v3 删除的函数及替代写法:
| 函数 | 替代 |
|---|---|
add_leading_zeros($no, $length) | str_pad($no, $length, '0', STR_PAD_LEFT) |
get_type_or_class($var) | get_debug_type($var) |
run_multi_process() | Ousaa\Utils\Library\Fork\ProcessPool |
get_rand_letlter_lower($length) | strtolower(UtilRandom::upperLetters($length)) |
行为变化:
UtilStr::snake/camel与 ThinkPHP 对齐:v2 的snake_case('FooBar')返回_foo_bar,camel_case('is_a')返回isa,都已修正UtilRandom::upperLetters($length)返回$length个字母,v2 的get_rand_letlter_capital不论传多少都只返回 1 个SimpleOutput::info等可以一次传多个参数,非字符串参数直接打印结构