Convert Object to JSON String in PHP

<?php 

class Student {
    public $id;
    public $name;
    public $age;
}

$student = new Student();
$student->id = 'st01';
$student->name = 'Name 1';
$student->age = 20;

header('Content-Type: application/json');
echo json_encode($student);

?>       
    
{"id":"st01","name":"Name 1","age":20}