PHP Classes

wrong message and login strict type checking sampe

Recommend this page to a friend!

      PHP Classes blog  >  PHP 7 Scalar Type Hin...  >  All threads  >  wrong message and login strict type...  >  (Un) Subscribe thread alerts  
Subject:wrong message and login strict type...
Summary:error does not fit well with promotion
Messages:5
Author:ing. conti
Date:2015-03-23 07:09:30
Update:2015-03-24 13:50:11
 

  1. wrong message and login strict type...   Reply   Report abuse  
Picture of ing. conti ing. conti - 2015-03-23 14:28:44
"in article we read:
At runtime, when the PHP engine tries to return a value, it will check if it matches the declared type and will throw a fatal error if does not match.
...
function increment(int $a): int { return $a + 1.0; // strictly type checked return }
..
Fatal error: Argument 1 passed to increment() must be of the type integer, string given"

a bit lower:
"Automatic promotion of types may also happen. For instance, int types can be promoted to float type parameters automatically"

so $a+1.0 IS CORRECT, it gives back a float, the error must be something like:

- cannot convert to Int without losing precison (as C/C++ does)
- function is of type int..
- WHY "string given" no string here.


Am I wrong?


  2. Re: wrong message and login strict type...   Reply   Report abuse  
Picture of Joseluis Laso Joseluis Laso - 2015-03-23 15:33:09 - In reply to message 1 from ing. conti
Hi ing.conti,

I think you are wrong.

I've just checked your code and, in strict_types=1 mode it throws an exception:
Fatal error: Return value of increment() must be of the type integer, float returned in /home/vagrant/test1/sum.php on line 7 in /home/vagrant/test1/sum.php on line 7

and in strict_types=0 mode it works fine.

Remember that, automatic promotion of types only works in weak mode (strict_types=0) and only with promotions that makes sense. For instance, if the functions waits an integer and you passes an string with "1" the function accept (converts) the parameter, or if the function waits a float and you send an integer, the same.

Please, replay if I don't understand well your question.

Thank you for your comment.

  3. Re: wrong message and login strict type...   Reply   Report abuse  
Picture of ing. conti ing. conti - 2015-03-23 19:45:53 - In reply to message 2 from Joseluis Laso
I KNOW what you say, is correct, I SIMPLY noted that the post have misleading samples AND that the error is NOT accurate.

only this..
:)

  4. Re: wrong message and login strict type...   Reply   Report abuse  
Picture of Ian Hartnell Ian Hartnell - 2015-03-23 21:40:35 - In reply to message 3 from ing. conti
ing. conti,

You are wrong and the original post is correct but not clearly worded.

The example begins with declare(strict_types=1) so strict mode is on.

If strict mode is on then "the strict type checking rules are quite straightforward: when the type of the value matches the one specified in the type declaration, it is accepted, otherwise it is not... The only exception is that scalar type conversion is allowed for int to float. This means that parameters that declare float can also accept int" (quote comes from later in the original post).

The example continues with two sample function definitions intended to show the syntax used.

function increment(int $a): int
{
return $a + 1.0; // strictly type checked return
}

class Foo
{
function increment(int $a): int
{
return $a + 1.0; // strictly type checked return
}
}

The original post then says "At runtime, when the PHP engine tries to return a value, it will check if it matches the declared type and will throw a fatal error if does not match."

The original post should then say, "For example, if you passed a string to these functions you would get the following error message:"

Fatal error: Argument 1 passed to increment() must be of the type integer, string given

The reference in the error message to 'Argument 1' is to the first (in this case only) argument passed , not to an argument with the value of 1.

I hope this helps.

Ian


  5. Re: wrong message and login strict type...   Reply   Report abuse  
Picture of Joseluis Laso Joseluis Laso - 2015-03-24 13:50:11 - In reply to message 3 from ing. conti
Hi ing conti.

I'm preparing a sample more complex and soon I'll publish in github.

Thank you for your comments.