I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP:
function blah(Bur $bur) {}
A couple of
It's called type hinting, added with PHP 5. It isn't quite what you may be expecting if you are coming from a language like Java. It does cause an error to be thrown if you don't pass in the expected type. You can't type-hint primitives, though (no int $bur).
#2 : (...) How can I declare the type of a variable in PHP if I'm not in a function?
I recently heard about "settype()" and "gettype()" in PHP4 & 5
You can force the variable type anytime easily
From PHP.net :
bool settype ( mixed &$var , string $type )
Parameters
var : The variable being converted. type : Possibles values of type are:
[ :D First visit, first comment...]
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.
Does this actually impose any limits on what type of variable I can pass to the blah method?
This is called type hinting. According to the PHP documentation that I just linked to, yes, it does impose limits on the argument type: "Failing to satisfy the type hint results in a catchable fatal error."
How can I declare the type of a variable in PHP if I'm not in a function?
Read type juggling. You can't explicitly define a variable's type in PHP, its type is decided by the context it is used in.
/**
* @var string
*/
public $variable = "Blah";