PHP class instance to JSON

后端 未结 5 2171
庸人自扰
庸人自扰 2020-12-08 09:28

I\'m trying echo the contents of an object in a JSON format. I\'m quite unexperienced with PHP and I was wondering if there is a predefined function to do this (like json_en

5条回答
  •  盖世英雄少女心
    2020-12-08 10:18

    You'll need to make your variable public, in order for them to appear on json_encode().

    Also, the code you're looking for is

    public function toJSON(){
        return json_encode($this);
    }
    

提交回复
热议问题