Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Question Weird type checking error!

    Hi all,

    I am running PHP 5.2.

    check out the following code:

    PHP 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



    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 ....

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    OIC ... well, thanks for that! The error message definitely is confusing!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •