bluray
bluray

Reputation: 1953

PHP - class member type

I have this code:

class X{

}

class Y{
    private $x;

    public function printx(){
        echo $this->x;
    }
}

PHPStorm hints on private $x:

Missing property's type declaration

But when I wrote type private X $x;, error in runtime:

Parse error: syntax error, unexpected 'X' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

Completed code is:

class X{

}

class Y{
    private X $x;

    public function printx(){
        echo $this->x;
    }
}

My PHP version is 7.3.6. Where is the problem? How to set type of class member?

Thanks

Upvotes: 1

Views: 192

Answers (1)

Francesco Simeoli
Francesco Simeoli

Reputation: 541

You can set the PHP runtime version in PhpStorm settings to get the right hints:

File -> Settings -> Languages & Frameworks -> PHP

In the main tab set these values:

- "PHP language level"
- "CLI Interpreter"

Upvotes: 1

Related Questions