<?php class Test implements \JsonSerializable { private $test; public function __construct(int $test) { $this->test = $test; } public function getTest(): int { return $this->test; } public function jsonSerialize(): array { $clone = new \stdClass; $clone->test = $this->getTest(); return (array) $clone; } } $testClass = new Test(1); $stdClass = new \stdClass; $stdClass->test = 1; $testClassEncode = json_encode($testClass); $stdClassEncode = json_encode((array) $stdClass); var_dump($testClassEncode, $stdClassEncode);
You have javascript disabled. You will not be able to edit any code.