Web Development & WordPress

PHP Object to JSON

Only Public ones will be converted

Code:

<?php 
class studentInfo{
    public $name;
    public $age;
    private $mobileNum;
public function setMobileNum($mobileNum){
    $this->mobileNum=$mobileNum;
}
public function getMobileNum(){
    return $this->mobileNum;
}


}
$student = new studentInfo();
$student->name= "Tom hanks";
$student->age = 45;
 $student->setMobileNum('11111111');
//echo $student->getMobileNum();
echo json_encode($student);
?>

Output:

{"name":"Tom hanks","age":45}

Leave a comment