Answer the question
In order to leave comments, you need to log in
API php, How to give data correctly?
Hello! I'm making an API and I've run into this problem. It is convenient to operate with all data inside the application with objects and classes. Let's say if this is a user's blog post - this is the class
class BlogPost extends Post{
// ......
}
Answer the question
In order to leave comments, you need to log in
It is best to implement the JsonSerializable interface, like this
class BlogPost extends Post implements JsonSerializable {
//...
public function jsonSerialize() {
return [
'title' => $this->title,
'attachments' => $this->attachments,
...
];
}
}
return json_encode($blogPost);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question