How to create a class to add two distances in PHP | PHP programs
Jan 19, 2022
PHP,
1184 Views
How to create a class to add two distances in PHP | PHP programs
How to create a class to add two distances in PHP | PHP programs
We will create a Distance class that contains feet and inches and then we add two distances using the Distance class.
<?php
class Distance
{
private $feet;
private $inch;
function SetDist($f, $i)
{
$this->feet = $f;
$this->inch = $i;
}
function PrintDist()
{
print ("Feet : " . $this->feet . '<br>');
print ("Inchs : " . $this->inch . '<br><br>');
}
function AddDist(Distance $d2)
{
$temp = new Distance();
$temp->feet = $this->feet + $d2->feet;
$temp->inch = $this->inch + $d2->inch;
if ($temp->inch >= 12)
{
$temp->feet++;
$temp->inch -= 12;
}
return $temp;
}
}
$d1 = new Distance();
$d1->SetDist(10, 2);
print ("Distance1 : " . '<br>');
$d1->PrintDist();
$d2 = new Distance();
$d2->SetDist(10, 3);
print ("Distance2 : " . '<br>');
$d2->PrintDist();
$d3 = $d1->AddDist($d2);
print ("Distance3 : " . '<br>');
$d3->PrintDist();
?>