|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits              [2010-10-19 12:53 UTC] uramihsayibok at gmail dot com
  [2010-10-20 10:46 UTC] justinmitchell at iinet dot net dot au
 
-Summary: Optional type hinting on variables
+Summary: Optional type hinting [strong typing] on variables
  [2010-10-20 10:46 UTC] justinmitchell at iinet dot net dot au
  [2010-10-20 17:15 UTC] rasmus@php.net
 
-Status: Open
+Status: Bogus
  [2010-10-20 17:15 UTC] rasmus@php.net
 | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Sun Oct 26 17:00:01 2025 UTC | 
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