Re: [RFC][Status] Scalar Type Declarations Voting Date Change

From: Date: Mon, 16 Mar 2015 02:36:23 +0000
Subject: Re: [RFC][Status] Scalar Type Declarations Voting Date Change
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-84972@lists.php.net to get a copy of this message
Hi all, Am 16.03.2015 um 03:16 schrieb Yasuo Ohgaki: > This code is an example that I posted in other thread. > > e.g. > <?php > function check_num_range(int $num) { if ($num < 0 || $num > 100) > trigger_error('Invalid range'); } > // Somewhere far from function definition. > $num = $GET['num']; > // Somewhere far from $num definition. > check_num_range($num); // Trying to check validity, int and range. > echo 'You have '.$num. ' now <br />'; // But $num could have any > string. > // > "check_num_range((int)$num)" wouldn't help also. > > Simple cast hides bugs, not eliminates type bugs. > This is just an example and there are many cases that cast hides bugs in > real world codes. please, if check_num_range() would be a build-in function, the outcome would be exactly the same. If $num contains something like "100 dogs", you get a notice: http://3v4l.org/fnuAc/rfc#tabs If $num contains rubbish, you get a catchable fatal error: http://3v4l.org/UStfP/rfc#tabs If you handle notices like errors, everything is fine for you. (And you should do that!) If you bring up an example to prove a point, please test it beforehand. If you have strict mode not enabled, there is exactly no incentive for you to always use casts. Greets Dennis

« previous php.internals (#84972) next »