How does PHP use the JsonSerializable interface?

I am confused about how it works JsonSerializable

. Let me tell you more about the problem.

We usually use the following interfaces:

<?php
interface Countable {
    /* Methods */
    public function count();    
}

class MyThings implements Countable 
{
    public function count() {
        return count($this->arrayOfThings);
    }
}


$obj = new MyThings();

//call count method on $obj
$obj->count();

      

So, we have a class and it implements the interface. When we call the function count()

, it is already written in the class MyThings

. It's easy to understand.

But when we use the interface JsonSerializable

like this:

<?php
class Thing implements JsonSerializable {
    public function jsonSerialize() {
        // do something 
    }
}
$obj = new Thing();

//call count method on $obj
json_encode($obj);

      

jsonSerialize()

internally Thing

executed with a call json_encode()

. This is understandable if we call

$obj->jsonSerialize();

      

then inside the class there is a function called jsonSerialize()

. But how does it work when we launch json_encode()

? How is this structured in php? Can someone please explain what patterns are used here?

+3


source to share


1 answer


Objects that are implement

JsonSerializable then implement the jsonSerialize () method. Then, when json_encode () serializes its input to JSON, if the value it serializes is JsonSerializable

, it invokes jsonSerialize()

and the result of this method is used as the serialized representation of the object.

For example, from the PHP documentation:

<?php
class IntegerValue implements JsonSerializable {
    public function __construct($number) {
        $this->number = (integer) $number;
    }

    public function jsonSerialize() {
        return $this->number;
    }
}

echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);

      

deduces

1

      



which is the value json_encode

d representing the number 1. There are three more such examples in the PHP documentation that return values ​​from an object, but since it jsonSerialize()

allows you to specify the actual data to be returned, it's important to understand that it can return anything. For example:

class JsonSerializeExample implements JsonSerializable {
    public function jsonSerialize() {
        return [
            'boolean' => true,
            'random_integer' => rand(),
            'int_from_object' => new IntegerValue(42),
            'another_object' => new stdClass,
        ];
    }
}
$obj = new JsonSerializeExample();
echo json_encode($obj, JSON_PRETTY_PRINT);

      

Will output

{
    "boolean": true,
    "random_integer": 1140562437,
    "int_from_object": 42,
    "another_object": {}
}

      

It should be noted that random_integer

it is not a static value stored anywhere; it changes with every execution; and int_from_object

demonstrates that it json_encode()

will evaluate instances recursively JsonSerializable

.

+1


source







All Articles