1

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

5
  • 1
    By updating to php7.4 Commented Sep 16, 2019 at 19:00
  • 2
    Or tell PhpStorm that you're using an older version of PHP, then it won't warn about this. Commented Sep 16, 2019 at 19:01
  • 1
    some light reading rfc typed properties Commented Sep 16, 2019 at 19:03
  • @u_mulder but php7.4 is not released. How to upgrade? Commented Sep 16, 2019 at 20:23
  • Release candidate exists. Commented Sep 16, 2019 at 20:36

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.