How to initialize data members without using the constructor in PHP | PHP program
Jan 18, 2022
PHP,
1165 Views
How to initialize data members without using the constructor in PHP | PHP program
How to initialize data members without using the constructor in PHP | PHP program
We will define a class Sample class with data members and then initialize private data members using the class method.
<?php
class Student
{
private $id;
private $name;
private $per;
function Initialize()
{
$this->id = 0;
$this->name = "";
$this->per = 0.0;
}
function Display()
{
print ("Student Id : " . $this->id . '<br>');
print ("Student Name : " . $this->name . '<br>');
print ("Student Percentage : " . $this->per . '<br>');
}
}
$S = new Student();
$S->Initialize();
$S->Display();
?>