php 强制参数类型,php – 如何强制参数为整数/字符串

Scalar TypeHints are available as of PHP 7:

Scalar type declarations come in two flavours: coercive (default) and strict. The following types for parameters can now be enforced (either coercively or strictly): strings (string), integers (int), floating-point numbers (float), and booleans (bool). They augment the other types introduced in PHP 5: class names, interfaces, array and callable.

PHP7之前没有标量的类型提示。 PHP 5.3.99 did have scalar typehints,但如果他们留下来,那么他们还没有最后确定,那么他们将如何工作。

然而,在PHP7之前,还有一些强制执行标量参数的选项。

有几个is_ *函数让你这样做,例如

要提出警告,你会使用

使用$ errorType的E_USER_WARNING。

function setInteger($integer)

{

if (FALSE === is_int($integer)) {

trigger_error('setInteger expected Argument 1 to be Integer', E_USER_WARNING);

}

// do something with $integer

}

替代

如果您想拼命使用标量类型提示,请查看

这显示了一种通过自定义错误处理程序强制执行标量类型提示的技术。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值