本文介绍了PHP - 强制函数参数为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
在 PHP 中你可以做以下事情:
In PHP you can do the following thing:
class Something {// bla bla}
function functionName(Something $object) {
// Do stuff here ^^^^^^^^^
}
这确保函数接收到Something类的实例.
This ensures that function received an instance of Something class.
我的问题是我想强制执行基本类型.
My problem is that I want to enforce to a basic type.
function functionName(integer $someInt) {
// Do stuff here ^^^^^^^
}
functionName(5);
这表示 $someInt 是不是整数的实例,给定的整数"(我的 PHP 版本低于 7.0).如何强制执行基本类型?
This says that $someInt is "not an instance of integer, integer given" (with my PHP version below 7.0). How can you enforce to a basic type?
推荐答案
您不能在 PHP 中键入提示标量类型.唯一的机会是使用包装器类型.为此,SPL 库附带了 SplInt.
You can't typehint scalar types in PHP. The only chance is to use a wrapper type. The SPL library ships with SplInt for that purpose.
更新:看起来 PHP 7 提供了该功能.唉,时代变了:)
Update: Looks like PHP 7 offers that feature. Sigh, times changed :)
这篇关于PHP - 强制函数参数为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)