Statically Typed vs. Dynamically Typed
Not to be confused with the dynamic type.
- Static typing is where the type is bound to the variable. Types are checked at compile time.
- Dynamic typing is where the type is bound to the value. Types are checked at run time.
So in Java for example:
String s = "abcd";
s
will “forever” be a String
. During its life it may point to different String
s (since s
is a reference in Java). It may have a null
value but it will never refer to an Integer
or a List
. That’s static typing.
In PHP:
$s = "abcd"; // $s is a string
$s = 123; // $s is now an integer
$s = array(1, 2, 3); // $s is now an array
$s = new DOMDocument; // $s is an instance of the DOMDocument class
That’s dynamic typing.