PHP: __toString() and json_encode() not playing well together

后端 未结 5 1924
故里飘歌
故里飘歌 2021-02-07 14:31

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

5条回答
  •  -上瘾入骨i
    2021-02-07 15:32

    Isn't your answer in the PHP docs for json_encode?

    For anyone who has run into the problem of private properties not being added, you can simply implement the IteratorAggregate interface with the getIterator() method. Add the properties you want to be included in the output into an array in the getIterator() method and return it.

提交回复
热议问题