Sunday, August 21. 2005
Typing PHP
Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Though I have somewhat used this in my own programming, I think you may have something there.
Very interesting.
Very interesting.
By making in_array more readable, the errors are easier to spot
. (in_array’s params are needle, haystack, so you switched the parameters).
What you propose is similar to the ‘hungarian notation’ (http://en.wikipedia.org/wiki/Hungarian_notation) that used to be common among programmers years ago. Instead of bar_int and bar_string you would write iBar and sBar.
There are several disadvantages to this in PHP however. Although you could consider this bad coding, it is valid, and PHP will not complain about the types:
$a = 5; // $a is now integer
$a = "hello ".$a; // $a is now a string
What you propose is similar to the ‘hungarian notation’ (http://en.wikipedia.org/wiki/Hungarian_notation) that used to be common among programmers years ago. Instead of bar_int and bar_string you would write iBar and sBar.
There are several disadvantages to this in PHP however. Although you could consider this bad coding, it is valid, and PHP will not complain about the types:
$a = 5; // $a is now integer
$a = "hello ".$a; // $a is now a string
Thanks, Ivo. I corrected this in the examples. Just another peculiarity of PHP, since most of the string functions are haystack then needle…
To respond to your concern about mutable types, I would recommend against changing types in PHP at all. That is:
$a_int = 5;
$b_string = "hello".$a;
I have seen many beginning programmers confuse themselves and others by altering the variable type in the course of their code.
To respond to your concern about mutable types, I would recommend against changing types in PHP at all. That is:
$a_int = 5;
$b_string = "hello".$a;
I have seen many beginning programmers confuse themselves and others by altering the variable type in the course of their code.
Visual Basic has a similar concept, the Variant datatype. (Note in VBA and VBScript, ALL variables are Variant types, in effect the same as PHP).
It’s a special datatype than can contain data of any type (even an object). VB naming conventions (based on hungarian) uses the prefix ‘vnt’ to indicate the variable is a variant.
So your example would become:
$vntA = 5; // $a is now integer
$vntA = "hello ".$vntA; // $a is now a string
In this way you are giving yourself a visible clue that the variable ‘can be any type - beware!’
Coming from a VB background I used to code my PHP using this format, but it just… looks… wrong :-S
It’s a special datatype than can contain data of any type (even an object). VB naming conventions (based on hungarian) uses the prefix ‘vnt’ to indicate the variable is a variant.
So your example would become:
$vntA = 5; // $a is now integer
$vntA = "hello ".$vntA; // $a is now a string
In this way you are giving yourself a visible clue that the variable ‘can be any type - beware!’
Coming from a VB background I used to code my PHP using this format, but it just… looks… wrong :-S
I’ve come across this before (even tried it out a bit), but I don’t really like it. It comes down to preference, of course. Well, that and an agreed on coding standard when working with others.
But I find the dynamic aspect of it helpful to have that flexibility. I don’t have a habit of switching types around, but I’d rather take advantage of the system rather than use it as statically typed.
By the same token, even though Objective-C has type declarations you could declare things as ‘id’ and name them foo_string. Or declare them as strings and name them foo_string as a reminder.
It seems like an idea that had a good reason for coming up, but not one that works for me.
But I find the dynamic aspect of it helpful to have that flexibility. I don’t have a habit of switching types around, but I’d rather take advantage of the system rather than use it as statically typed.
By the same token, even though Objective-C has type declarations you could declare things as ‘id’ and name them foo_string. Or declare them as strings and name them foo_string as a reminder.
It seems like an idea that had a good reason for coming up, but not one that works for me.

