PHP Classes

integers are acceptable as float input

Recommend this page to a friend!

      PHP Classes blog  >  How PHP 7 Can Help Yo...  >  All threads  >  integers are acceptable as float input  >  (Un) Subscribe thread alerts  
Subject:integers are acceptable as float input
Summary:Note that an integer will be acceptable for float type-hinting.
Messages:2
Author:chrisp
Date:2016-05-29 22:37:43
Update:2016-05-29 23:53:21
 

  1. integers are acceptable as float input   Reply   Report abuse  
Picture of chrisp chrisp - 2016-05-29 23:19:27
This means that, for example, using an integer, such as 4, instead of a float, such as 4.0, will not generate an error when a function has a float type-hinted parameter. Obviously, a float will generate an error when an int is required. I've created a few methods that enforce a float to actually be a float during math operations (and not an integer), by checking each parameter using is_float() and then passing the "validated" parameter to the real type-hinted float math function. Check it out:

github.com/chrispecoraro/PHPStrict

  2. Re: integers are acceptable as float input   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2016-05-29 23:53:21 - In reply to message 1 from chrisp
You are right a integer can also be a float and it should not trigger a type exception.

Interesting that PHP Strict class. You may want to submit it to PHPClasses so we can have more examples that take advantage of PHP 7.

There is already a package group for PHP 7 but not many examples so far:

phpclasses.org/browse/class/360.htm ...