TomTees Posted October 2, 2010 Share Posted October 2, 2010 Is it possible to define a variable's type at the start of your code? I believe you can "cast" a variable, but can I do something like this... name string; age integer; gender string; children array; single: boolean; I know some people say PHP is better suited for the web because it is loosely-typed, but I'm anal-retentive and like my structure!! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214982-defining-variable-types/ Share on other sites More sharing options...
Alex Posted October 2, 2010 Share Posted October 2, 2010 Nope, you can't do that. Semi-related: You can do type-hinting in PHP 5.1+ in object methods: http://php.net/manual/en/language.oop5.typehinting.php edit: You could also check out the experimental SPL Types extension: http://pl.php.net/manual/en/book.spl-types.php Quote Link to comment https://forums.phpfreaks.com/topic/214982-defining-variable-types/#findComment-1118304 Share on other sites More sharing options...
TomTees Posted October 2, 2010 Author Share Posted October 2, 2010 Nope, you can't do that. Semi-related: You can do type-hinting in PHP 5.1+ in object methods: http://php.net/manual/en/language.oop5.typehinting.php edit: You could also check out the experimental SPL Types extension: http://pl.php.net/manual/en/book.spl-types.php Are there any plans to support the information in the links above in any future versions of PHP? (I would have thought PHP 6 would include that?!) Also, what if I did this... $name = 0; $name = (string) $name; OR // Define bogus variables $a=$b=$c=$d=0; $s = 's'; $a = array(); $b = TRUE; // Cast bogus variables into desired Data-Types $name = (string) $a; $age = (integer) $s; $gender = (string) $b; $children = (array) $c; $single = (boolean) $d; I realize that may seem like a nonsensical approach, but it would allow you to have defined data-types, right? What do you think? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214982-defining-variable-types/#findComment-1118333 Share on other sites More sharing options...
TomTees Posted October 3, 2010 Author Share Posted October 3, 2010 Any thoughts on this? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214982-defining-variable-types/#findComment-1118585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.