This type-hinting only works for validating function arguments, return values, properties, and class constants; 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 works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.
The type can also be made nullable by adding ? before it. This is identical to a type union with null.
foo(?string $var) { }
// is the same as
foo(string|null $var) { }
You can also use composite types such as string|int as of PHP 8.0.
An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.
Answer from JW. on Stack OverflowVideos
This type-hinting only works for validating function arguments, return values, properties, and class constants; 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 works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.
The type can also be made nullable by adding ? before it. This is identical to a type union with null.
foo(?string $var) { }
// is the same as
foo(string|null $var) { }
You can also use composite types such as string|int as of PHP 8.0.
An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.
- Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
- Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.
/**
* @var string
*/
public $variable = "Blah";
As of PHP 7 (which is several years old at this point), primitive types can also be declared for function arguments. Nullability can also be indicated with a ? in front of the type from 7.1 onward. You can declare return types now, too. So this is valid PHP these days:
public function hasFoo(?int $numFoos) :bool {
PhpStorm (my current preferred IDE) is happy to use all of these types for code completion, so I don't need as many phpDoc comments for typing as I used to.