<?php
trait JSONized {
public function toJson()
{
$properties = get_object_vars($this);
return json_encode($properties);
}
}
class User
{
use JSONized;
public $username;
public $password;
public $email;
public function __construct($username, $password, $email)
{
$this->username = $username;
$this->password = md5($password);
$this->email = $email;
}
}
$emran = new User('phpfour', 'emran123', 'phpfour@gmail.com');
echo $emran->toJson();