PHP7函数类型限定是什么意思,有什么影响吗
Admin 2022-07-30 群英技术资讯 1528 次浏览
本篇内容介绍了“PHP7函数类型限定是什么意思,有什么影响吗”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
本文主要通过简单的压测来探讨PHP7函数数据类型限定设定与否对性能的影响,另外,分享下自己两个工作中遇到的小问题及其应对,如有错误,恳请指正。
作用
推荐学习:《PHP视频教程》
function testInt(int $intNum){
var_dump($intNum);
}
testInt("123"); // int(123)运行环境
单机配置
AB
主要参数
/***** 1 普通接口 *****/
// CommonUserController
public function createUser(Request $request)
{
$this->validate($request, [
'name' => 'required|string',
'age' => 'required|integer',
'sex' => ['required', Rule::in([1, 2])],
]);
(new CommonUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
return response()->json(['status' => 200, 'msg' => 'ok']);
}
// CommonUserModel
public function createUser($sex, $age, $name, $address)
{
if(empty($sex) || empty($age) || empty($name)) return false;
// 省略DB操作
return true;
}
/***** 2 类型限定接口 *****/
// TypeUserController
public function createUser(Request $request): JsonResponse
{
$this->validate($request, [
'name' => 'required|string',
'age' => 'required|integer',
'sex' => ['required', Rule::in([1, 2])],
]);
(new TypeUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
return response()->json(['status' => 200, 'msg' => 'ok']);
}
// TypeUserModel
public function createUser(int $age, string $name, int $sex, string $address): bool
{
if(empty($sex) || empty($age) || empty($name)){
return false;
}
// 省略DB操作
return true;
}/*****第一次*****/ // 类型限定接口 rps=456.16 ab -n 100 -c 10 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=450.12 ab -n 100 -c 10 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/common/create_user /*****第二次*****/ // 类型限定接口 rps=506.74 ab -n 1000 -c 100 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=491.24 ab -n 1000 -c 100 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/common/create_user /*****第三次*****/ // 类型限定接口 rps=238.43 ab -n 5000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=237.16 ab -n 5000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user /*****第四次*****/ // 类型限定接口 rps=209.21 ab -n 10000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=198.01 ab -n 10000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user /*****第五次*****/ // 类型限定接口 rps=191.17 ab -n 100000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=190.55 ab -n 100000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
类型限定对性能的提升没有预期的大,很微小,不过还是推荐这种写法
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
1.php的版本:PHP7.3.0-dev(cli)(built:Mar18201800:28:55)(NTS)2.unless语法结构:unless($cond){statement;}语法作用:当$cond为假的时候,执行statement语句。3.代码修改词法分析文件:Zend/zend_langua
php正则替换函数的整理:1、preg_filter函数用于执行一个正则表达式搜索和替换。2、preg_replace_callback函数执行一个正则表达式搜索并且使用一个回调进行替换。
laravel竟是最慢框架,比lumen慢了一倍?就连tp都比laravel快50%?这究竟是怎么回事呢?来这里看框架性能大对比,看看比对过程。
今天本栏目介绍PHP7进行数据库操作,讲解连接、增删改查的操作,有需要的朋友可以看看,有问题可以评论讨论。
如何访问 PHP 的文件系统?使用 PHP 的文件系统函数:Filesystem 函数,有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008