This type-hinting only works for validating function arguments; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when "blah" is called, but $bur could be reassigned to a non-Bur value inside the function.
Type-hinting only works for class or interface names; you can't declare that an argument must be an integer, for example.
One annoying aspect of PHP's type-hinting, which is different from Java's, is that NULL values aren't allowed. So if you want the option of passing NULL instead of an object, you must remove the type-hint and do something like this at the top of the function:
assert('$bur === NULL || $bur instanceof Bur');
EDIT: This last paragraph doesn't apply since PHP 5.1; you can now use NULL as a default value, even with a type hint.
EDIT: You can also install the SPL Type Handling extension, which gives you wrapper types for strings, ints, floats, booleans, and enums.
EDIT: You can also use "array" since PHP 5.1, and "callable" since PHP 5.4.
EDIT: You can also use "string", "int", "float" and "bool" since PHP 7.0.
EDIT: As of PHP 7.4, you can declare member variables of a class/interface/trait as a specific type like public int $a;
, and variables that are declared this way cannot be assigned to a value of another type. You can also use union types such as string|int
as of PHP 8.0, and you can use classes in the union types as of PHP 8.1.
https://www.php.net/manual/en/language.types.declarations.php