Sharp House

  • Increase font size
  • Default font size
  • Decrease font size

Strongly typing in PHP

E-mail

Since when I started programming with PHP 3, it has always been a weakly typed programming language.

This means, in a nutshell, that you can declare a variable and assign it any value, letting the interpreter guess what type it is at runtime.

This has both advantages and disadvantages: it lets you build simple applications very quickly, switch variables' type at will during runtime, use different input types the same way (just think of the string '15.6' and the corresponding float value 15.6, PHP will add any number to either correctly, no explicit conversion required), and the list goes on.

Strongly typing, on the other hand, allows better management of large projects, as any additional check to find that last sneaky bug is always welcome, and comes with a number of different features that PHP is missing, or not implementing completely, namely functions polymorphism.
This implicates that it's not possible to have 2 functions with the same name and different type parameters, changing the actual function called based on the types of the parameters we give it.

Does this mean that it's not possible to use the best of the 2 worlds when we want to? The answer is yes and no - and I'm not talking about the tons of different workarounds that you'll find around the web, made to try and implement languages' abilities that PHP is missing.

Take a look at this code:

Let's see how PHP supports strong typing
 


« July 2012 »
Mo Tu We Th Fr Sa Su
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31