moitran / php-common-funcs
编写有用的PHP常用函数
v1.0.1
2018-12-18 07:39 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.3
- squizlabs/php_codesniffer: ^3.3
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-09-18 20:46:03 UTC
README
编写有用的PHP常用函数
安装
$ composer require moitran/php-common-funcs
它有什么?
-
数组
-
日期时间
- getCurrentTime 获取当前时间,带有格式参数。
- getNow 返回当前Unix时间戳。
- format 接收日期字符串参数,并以指定格式返回。
- getListRangeDate 返回指定格式下两个日期之间的日期列表。
- getPreviousDates 返回指定格式下具有参数的天数之前的日期。
- getPreviousDateRange 返回与日期范围参数相反的先前日期列表。
- getTimeBetweenTwoDate 返回两个日期之间的秒数(S)、分钟(M)、小时(H)或天数(D)。
- getAge 通过出生日期返回年龄。
- niceTime 返回美观的时间格式。
最佳实践(个人看法)
此包中的所有函数都具有静态方法,因此如果我们以这种方式调用函数,将很难编写phpunit测试代码。
use MoiTran\CommonFunctions\DateCommonFunctions; class A { public function doSomeThing() { $time = DateCommonFunctions::getCurrentTime('Y/m/d'); // do some thing } }
解决方案是使用此特征StaticCalling来调用这些静态函数。代码如下所示
use MoiTran\CommonFunctions\DateCommonFunctions; use MoiTran\CommonFunctions\StaticCalling; class A { use StaticCalling; public function doSomeThing() { $time = $this->callStatic(DateCommonFunctions::class, DateCommonFunctions::GET_CURRENT_TIME_FUNC, ['Y/m/d']); // do some thing } }
许可证
本软件包采用MIT许可证