RFC: Strict comparisons

From: Date: Mon, 19 Sep 2016 13:04:48 +0000
Subject: RFC: Strict comparisons
Groups: php.internals 
Request: Send a blank email to internals+get-96028@lists.php.net to get a copy of this message
Hey all, I've been bouncing this idea of fixing all comparison operations in one fell swoop, although with an opt-in declare in the spirit of strict_types. Here's the gist: ==, < and > plus all the comparisons done inside switch statements and arrays are not very safe. === helps some, but does nothing for switches and < >. My idea is to add a strict_comparisons declaration that you can add at the beginning of a file in the same way as strict_types. The effect would be that all normal comparisons would make a type check before doing the actual comparison, and throw a TypeError exception if the types don't match. In other words: <?php declare(strict_comparisons=1); $two = "2"; if ($two > 1) { echo "This sorta works without strict_comparisons=1" . PHP_EOL; } ...would throw a TypeError exception after this change. This change would mostly go into zend_operators.c compare_function. So it would affect every comparison operation, including switch statements and things like in_array. I've implemented a prototype with a couple of test cases in https://github.com/paytrail/php-src/tree/experimental/strict_comparisons Please take a look and let me know if this would be worth a more official RFC. <https://github.com/paytrail/php-src> --vk

« previous php.internals (#96028) next »