...

View Full Version : Weird type checking error!



mlse
07-22-2007, 02:28 PM
Hi all,

I am running PHP 5.2.

check out the following code:



function foo(integer $arg)
{
echo $arg."\n";
}

$num = 1;

try
{
foo($num);
}
catch (Exception $e)
{
echo $e->getmessage()."\n";
}


Perfectly resonable, you might say ... but when I run it, I get the following error:

Catchable fatal error: Argument 1 passed to foo() must be an instance of integer, integer given, called in /***/test.php on line 12 and defined in /***/test.php on line 3

:confused: :confused: :confused:

Incidentally it works fine when I strictly type the function argument to be an object - it just doesn't seem to like non-object types or references ....

GJay
07-22-2007, 02:45 PM
you can only type-hint with objects and arrays. The error message in your case is a little odd, but the two uses of 'integer' refer to different things. The first is your type hint, and would be satisfied if you made a class yourself named 'integer' (it's not a reserved word) the second refers to the type of the argument, as PHP sees it.

mlse
07-22-2007, 02:56 PM
OIC ... well, thanks for that! The error message definitely is confusing!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum