How to define a PHP variable as a specific class type before instantiate an object?

Depending on the IDE, you can use PHPDoc comments to get code completion

 /** @var YourClass */
 private $variable;

This is IDE specific though

Edit and 6 years later (almost to the day), with the advent of php 7.4, you can now declare types for class properties:

Class SomeClass
{

    private YourClass $variable;

    public string $something_else;

}

PHP is loosely typed, so you can't do it like you do in Java. You can provide type hinting for custom classes, though. See this description on PHP.net: http://php.net/manual/en/language.oop5.typehinting.php

Tags:

Php

Types