JSONize your PHP classes using traits

I was having a discussion with a colleague regarding how to add generic JSON based representation to a number of classes without making a big effort. The immediate solution that came in my mind is to use the Traits for it (introduced in PHP 5.4).

So I wrote the following example for him:

JSONized.php

User.php

username = $username; $this->password = md5($password); $this->email = $email; } }

test.php

toJson();

The result of running the above code will be like this:

{ "username" : "phpfour", "password" : "02c2ec1e3f21e97dd5ac747147a141e0", "email" : "" }