Re: Scalar Type Hints v0.4

From: Date: Wed, 18 Feb 2015 15:34:02 +0000
Subject: Re: Scalar Type Hints v0.4
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-83084@lists.php.net to get a copy of this message
Hi Sara (and thanks for continuing the work!) Le Tue Feb 17 2015 at 23:04:20, Sara Golemon <pollita@php.net> a écrit : [...] > 2) Define a way to enable "strict mode" (we'll be weak by default). > [...] > 3) Tighten up coersion rules for weak mode. i.e. "10 dogs" for an int > is a violation, but "10" is acceptable. > Regarding 2) and 3): An option might be to implement "weak mode" only and configure the coercion rules "reporting" in a similar way than with the error_reporting configuration entry. Not focusing on the details here, but I think about something like: function foo(int $a) { var_dump($a); } ini_set("coercion_reporting", 0); // current PHP 5.x behaviour foo(7); // int(7) foo("7"); // int(7) foo("7 dogs"); // int(7) ini_set("coercion_reporting", COERCION_WARNING); // Warn, but do not fail in case of potentially bad coercion foo(7); // int(7) foo("7"); // int(7) foo("7 dogs"); // Warning: Unsafe coercion transforming "7 dogs" to "7". // int(7) ini_set("coercion_reporting", COERCION_ERROR); // Fail in case of potentially bad coercion foo(7); // int(7) foo("7"); // int(7) foo("7 dogs"); // Catchable fatal error: Unsafe coercion transforming "7 dogs" to "7". The biggest advantage, IMHO, is that you get the exact same result whether you do: foo((int) $value); or: foo($value); ... whatever the mode you are in. Basically, this is weak type hints + something similar to what I contributed in the past with the "Array to string conversion" notice (see: https://github.com/php/php-src/commit/d81ea16e). Care to share the pro's/con's you see with a solution like that? Thanks in advance. Cheers, Patrick

« previous php.internals (#83084) next »