PHP:将整数转换为int-出现错误 [英] PHP: Converting integer to int - getting error

查看:55
本文介绍了PHP:将整数转换为int-出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道PHP中int和integer之间没有什么不同。我一定是错的。

I understood there wasn't much, if any different between int and integer in PHP. I must be wrong.

我正在将整数值传递给仅具有该值的整数的函数。像这样:-

I'm passing a integer value to a function which has int only on this value. Like so:-

$new->setPersonId((int)$newPersonId); // Have tried casting with (int) and intval and both

进行投射/ p>

The other side I have:-

    public function setPersonId(int $value) {
        // foobar
    }

现在,当我跑步时-我收到以下消息:-

Now, when I run - I get the message:-

PHP可捕获的致命错误:传递给setPersonId()的参数1必须是int的实例,给定整数

我尝试过强制转换

有什么想法吗?

推荐答案

PHP中的类型提示仅适用于对象,而不适用于标量,因此PHP希望您传递的对象类型为 int。

Type hinting in PHP only works for objects and not scalars, so PHP is expecting you be passing an object of type "int".

您可以使用以下是解决方法

You can use the following as a workaround

public function setPersonId($value) {
    if (!is_int($value)) {
        // Handle error
    }
}

这篇关于PHP:将整数转换为int-出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆