From: Operating system: Nix/Win PHP version: 5.3.3 Package: Java related Bug Type: Feature/Change Request Bug description:Optional type hinting on variables
Description: ------------ I know there's been a lot of discussion about type hinting for years now. It's prevalent in most OOP languages and as PHP makes the move towards being an OOP scripting language, it would make sense to introduce certain language features. Unless there is a technical constraint (quite possible), would it be possible to introduce type hinting on variables? I make the argument because it would be encourage data validation based on the variable type rather than having to create functions to check if the input matches the variable type. It would vastly improve current and future frameworks, reduce code and improve error handling (assuming the programmer implements). Cast exceptions would need to be implemented as well, eg. String could not be cast to int, int cannot be set a string value etc. It can also in part replace the is_int, is_numeric, is_bool etc. functions; functions which wouldn't be needed if there was variable type hinting. Test script: --------------- <?php $a = 0; int $b = 0; changeVar(&$var) { try { $var = 'abc123'; } catch (Exception $ex) { echo 'could not change variable value or something'; } } Expected result: ---------------- There will be a compile time error because variable type hinting isn't supported. If it were to be implemented you would expect something along the lines of: changeVar($a); changeVar($b); // prints exception message echo $a; // abc123 echo $b; // 0 Actual result: -------------- Throws a compile time error, isn't supported by php -- Edit bug report at http://bugs.php.net/bug.php?id=53095&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53095&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53095&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53095&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=53095&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53095&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=53095&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=53095&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=53095&r=needscript Try newer version: http://bugs.php.net/fix.php?id=53095&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=53095&r=support Expected behavior: http://bugs.php.net/fix.php?id=53095&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=53095&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=53095&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=53095&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53095&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=53095&r=dst IIS Stability: http://bugs.php.net/fix.php?id=53095&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=53095&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=53095&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=53095&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=53095&r=mysqlcfg