Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Converting integer to int - getting error

Tags:

php

casting

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

The other side I have:-

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

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

"PHP Catchable fatal error: Argument 1 passed to setPersonId() must be an instance of int, integer given"

I have tried casting in the call with (int) and intval().

Any ideas?

like image 619
waxical Avatar asked Jan 10 '13 13:01

waxical


1 Answers

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
    }
}
like image 130
fin1te Avatar answered Oct 04 '22 11:10

fin1te