Re: What do we need strict scalar type hints for?
| From: | Andrey Andreev | Date: | Mon, 02 Feb 2015 21:18:12 +0000 |
| Subject: | Re: What do we need strict scalar type hints for? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81655@lists.php.net to get a copy of this message | ||
Hi,
On Mon, Feb 2, 2015 at 11:03 PM, Thomas Bley <mails@thomasbley.de> wrote:
> Here is a typical billing example which uses exceptions:
>
> <?php
> ini_set('error_reporting', E_ALL);
> ini_set('display_errors', 1);
>
> addVat('apples');
>
> function addVat($amount) {
> if (!is_int($amount) && !is_float($amount)) {
> throw new InvalidArgumentException('Argument 1 passed to '.__FUNCTION__.'
> must be of the type int|float, '.gettype($amount).' given');
> }
> return round($amount*1.19, 2);
> }
>
> Instead of multiple strict scalar hints (e.g. function addVat(int|float $amount){...}), I would
> prefer to have "numeric" as strict type:
> function addVat(numeric $amount){...}
>
That is an example where a weak hint to float would be better.
Here's a more appropriate example that I gave in the other thread:
http://marc.info/?l=php-internals&m=142142272705938&w=2
Cheers,
Andrey.