When we want to give common functionality to the Object during creation of the Object then this functionality must be written into the constructor.
Example:-
class interestCalculator{
public $rate;
public $duration;
public $capital;
public function __construct($rate, $duration,$capital) {
$this->capital=$capital;
$this->rate=$rate;
$this->duration=$duration;
$SI=($this->capital*$this->rate*$this->duration)/100;
echo $SI;
}
}
$obj=new interestCalculator(3.2,7,2800);
Copyright ©2022 coderraj.com. All Rights Reserved.