在Windows 10上找不到PHP 5.6.8 JsonSerializable接口

时间:2016-05-20 14:17:31

标签: php json

我试图在下面的类中实现JsonSerializable接口:

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

1 个答案:

答案 0 :(得分:4)

你需要这样做:

class PostoGraduacao implements \JsonSerializable

从错误消息中,您可以看到PHP正在寻找:

PostoGraduacao\Model\JsonSerializable

这是因为您的类位于PostoGraduacao\Model命名空间内。使用\JsonSerializable告诉PHP查看全局命名空间。