PhpStorm does not recognize properties type hinting for PHP 7.4

≯℡__Kan透↙ 提交于 2019-12-12 06:46:37

问题


Recently I found out that with PHP 7.4 we can type-hint properties in classes.

Here is my code

class Tournament {
    private Organization $organization;
    private array $teams;

    public function __construct() {
        $this->teams = array();
    }

    public function setOrganization(Organization $organization) {
        if (!$this->organization->equals($organization)) {
            $this->organization = $organization;
        }
    }
}

I use PhpStorm and it gives me a warning for ($this->organization->equals(..)), saying that the referenced method is not found; but my Organization class clearly defines such method:

class Organization {
    private array $tournaments;
    private array $users;
    private string $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getTournaments()
    {
        return $this->tournaments;
    }

    public function getUsers()
    {
        return $this->users;
    }

    // ...

    public function equals(Organization $organization) {
        return $organization->getName() == $this->getName();
    }
}

So why is there a problem in the Tournament class?. I've specified the type so I think this should work.


回答1:


Type hinting will be available for properties as of PHP 7.4.

But you need to configure PhpStorm so it knows what version of the language you are using.

Go to Settings->Languages & Frameworks->PHP and set the language level there so it specifies "PHP 7.4". Like this:



来源:https://stackoverflow.com/questions/59029713/phpstorm-does-not-recognize-properties-type-hinting-for-php-7-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!