Is there any way I can set up PHP objects so that when I try to convert them to JSON, all of their protected properties will be shown?
I have read other answers suggesting I add a toJson() function to the object, but that may not really help me a great lot. In most cases, I have an array of objects and I perform the encode on the array itself.
$array = [
    $object1, $object2, $object3, 5, 'string', $object4
];
return json_encode($array);
Yes, I can loop through this array and call toJson() on every element that has such method, but that just doesn't seem right.  Is there a way I can use magic methods to achieve this?
The json_encode() function is used to encode a value to JSON format.
To encode objects into a JSON formatted string in PHP, you can use the json_encode(value, options, depth) function. The first parameter specifies the PHP object to encode. You can control how the PHP object will be encoded into JSON by passing a combination of bitmasks in the second parameter.
Parsing JSON data in PHP: There are built-in functions in PHP for both encoding and decoding JSON data. These functions are json_encode() and json_decode(). These functions works only with UTF-8 encoded string. Decoding JSON data in PHP: It is very easy to decode JSON data in PHP.
json_encode — Returns the JSON representation of a value.
You can implement the JsonSerializable interface in your classes so you have full control over how it is going to be serialized. You could also create a Trait to prevent copy pasting the serializing method:
<?php
trait JsonSerializer {
    public function jsonSerialize()
    {
        return get_object_vars($this);
    }
}
class Foo implements \JsonSerializable 
{
    protected $foo = 'bar';
    use JsonSerializer;
}
class Bar implements \JsonSerializable 
{
    protected $bar = 'baz';
    use JsonSerializer;   
}
$foo = new Foo;
$bar = new Bar;
var_dump(json_encode([$foo, $bar]));
Alternatively you could use reflection to do what you want:
<?php
class Foo
{
    protected $foo = 'bar';
}
class Bar
{
    protected $bar = 'baz';
}
$foo = new Foo;
$bar = new Bar;
class Seriailzer
{
    public function serialize($toJson)
    {
        $data = [];
        foreach ($toJson as $item) {
            $data[] = $this->serializeItem($item);
        }
        return json_encode($data);
    }
    private function serializeItem($item)
    {
        if (!is_object($item)) {
            return $item;
        }
        return $this->getProperties($item);
    }
    private function getProperties($obj)
    {
        $rc = new ReflectionClass($obj);
        return $rc->getProperties();
    }
}
$serializer = new Seriailzer();
var_dump($serializer->serialize([$foo, $bar]));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With