Luiz Eduardo de Christo Luiz Eduardo de Christo - 6 months ago 47
PHP Question

PHP 5.6.8 JsonSerializable interface not found on Windows 10

I'm trying to implement the JsonSerializable interface on the class below:

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
];
}
}


My Php version:

C:\>php -v
PHP 5.6.8 (cli) (built: Apr 15 2015 15:07:09)
Copyright (c) 1997-2015 The PHP Group


And the error:

Fatal error: Interface 'PostoGraduacao\Model\JsonSerializable' not found inC:\Projetos\RESTServices\src\PostoGraduacao\model\PostoGraduacao.php on line 11

Answer

You need to do:

class PostoGraduacao implements \JsonSerializable

From the error message, you can see that PHP is looking for:

PostoGraduacao\Model\JsonSerializable

This is because your class is located inside the PostoGraduacao\Model namespace. Using \JsonSerializable tells PHP to look in the global namespace.

Comments