PHP Object JSON representation
<?php
class Comment implements jsonSerializable{
public $author;
public $content;
public $date;
public function jsonSerialize(){
return array(
"author" => $this->author,
"content" => $this->content,
"date" => $this->date
);
}
}
$myComment = new Comment();
$myComment->author = "Abimael Martell";
$myComment->content = "Hi there";
$myComment->date = "2012-11-13 21:06:12";
echo json_encode($myComment);
//{
// "author":"Abimael Martell",
// "content":"Hi there",
// "date":"2012-11-13 21:06:12"
//}
?>
Written by Abimael Martell
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#