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
Did you know?
PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#