1
In PHP there is a problem regarding the serialization of the object. Usually this is solved when the class that attempts to serialize implements the interface JsonSerializable
.
class Obj implements JsonSerializable
{
protected $items = array();
public funciton __construct($items)
{
$this->items = $items;
}
public function jsonSerialize()
{
return $this->items;
}
}
json_encode(new Obj(array('1', '2', '3')); // "[1, 2, 3]"
I realized that some classes, even if they are data structures, do not implement this interface. And as a result I had problems - what I didn’t have in ArrayObject
, even if it also does not implement JsonSerializable
.
Behold:
$a = new Splstack;
$a[] = 1;
$a[] = 2;
$a[] = 3;
var_dump(json_encode($a)); //Imprime: "[]"
As you can see SplStack
returned nothing as serialized by json_encode
.
Why does this happen to this particular class? It would be a bug?
There arises another doubt http://answall.com/questions/92495/comorarrayobject-%C3%A9-serialized-by-json-Encode-j%C3%A1-that-same-n%C3%A3o-implementa-js
– Wallace Maxters