PHP: __toString() and json_encode() not playing well together
问题 I've run into an odd problem and I'm not sure how to fix it. I have several classes that are all PHP implementations of JSON objects. Here' an illustration of the issue class A { protected $a; public function __construct() { $this->a = array( new B, new B ); } public function __toString() { return json_encode( $this->a ); } } class B { protected $b = array( 'foo' => 'bar' ); public function __toString() { return json_encode( $this->b ); } } $a = new A(); echo $a; The output from this is [{},{