我正在尝试在下面的类上实现 JsonSerialized 接口(interface):
class PostoGraduacao implements JsonSerializable
{
private $index;
private $abbreviation;
private $rank;
public function __construct($index, $abbreviation, $rank){
$this->index = $index;
$this->abbreviation = $abbreviation;
$this->rank = $rank;
}
public function jsonSerialize()
{
return[
'index' => $this->index,
'abbreviation' => $this->abbreviation,
'rank' => $this->rank
];
}
}
我的 PHP 版本:
C:\>php -v
PHP 5.6.8 (cli) (built: Apr 15 2015 15:07:09)
Copyright (c) 1997-2015 The PHP Group
错误:
Fatal error: Interface 'PostoGraduacao\Model\JsonSerializable' not found inC:\Projetos\RESTServices\src\PostoGraduacao\model\PostoGraduacao.php on line 11
最佳答案
你需要做:
class PostoGraduacao implements \JsonSerializable
从错误消息中可以看到PHP正在寻找:
PostoGraduacao\Model\JsonSerializable
这是因为您的类位于 PostoGraduacao\Model
命名空间内。使用 \JsonSerialized
告诉 PHP 在全局命名空间中查找。
关于在 Windows 10 上找不到 PHP 5.6.8 JsonSerialized 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37349099/