问题
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