Loading, please wait...

A to Z Full Forms and Acronyms

How to create multiple objects of a class and access attributes of the class in PHP | PHP program

Jan 18, 2022 PHP, 1328 Views
How to create multiple objects of a class and access attributes of the class in PHP | PHP program

How to create multiple objects of a class and access attributes of the class in PHP | PHP program

We will create a class Student and then create multiple objects of class and access all attributes of the class and then print the values attributes on the web page.

<?php
//PHP program to create multiple objects of a class and access class attributes
class Student
{
    //Attributes
    public $id;
    public $name;
    public $per;
}

$S1 = new Student();
$S2 = new Student();

$S1->id = 500;
$S1->name = "Rajib";
$S1->per = 65.20;

$S2->id = 600;
$S2->name = "Rahul Sharma";
$S2->per = 88.50;

print ("Student1:" . '<br>');
print ("--->Student Id  		  : " . $S1->id . '<br>');
print ("--->Student Name		  : " . $S1->name . '<br>');
print ("--->Student Percentage    : " . $S1->per . '<br>');

print ("Student2:" . '<br>');
print ("--->Student Id  		  : " . $S2->id . '<br>');
print ("--->Student Name		  : " . $S2->name . '<br>');
print ("--->Student Percentage    : " . $S2->per . '<br>');

?>
A to Z Full Forms and Acronyms