Type hinting in class variables

PHP 7.3 and below does not support typed properties. You could only define a variable as below:

class Sandbox {
    private $connection;

However, to help editors understand your code, you may use a @var tag to document the expected type of the property:

class Sandbox {
    /** @var Connectors\ISandboxConnector */
    private $connection;

Update

PHP 7.4.0

Thanks @Manuel for mentioning the new update, PHP 7.4 now introduces typed properties according to PHP RFC: Typed Properties 2.0.

Property type declarations support all type declarations supported by PHP, with the exception of void and callable. Any class or interface name, stdClass, scalar and compound types, references to parent and own objects are also supported.

class Sandbox {
    public int $id;
    public string $name;
    private Connectors\ISandboxConnector $connection;
}

Note: keep an eye on side effects such as uninitialised state and inheritance strict rules.


Since PHP 7.4 you are able to type hint on class properties, as shown here. So for example your $connection property would look like this:

class Sandbox {
    private Connectors\ISandboxConnector $connection;
}