<?php
error_reporting(E_ALL | E_STRICT);

class CreditCalculator
{

    private $creditSum;
    private $payout;
    
 function __construct($creditSum, $payout)
{
     $this->creditSum = $creditSum;
     $this->payout = $payout;
 }
 
public function calculateCredit($creditRate, $comission, $openingPayment)
{
    $creditSum = $this->creditSum + $openingPayment;
    $totalPaid = $openingPayment;
            
    while($creditSum > 0)
   {
        $creditMonthlyPercent = ($creditSum / 100) * $creditRate;  
        $creditSum = $creditSum + $creditMonthlyPercent + $comission; 
        
        if ($this->payout <= $creditSum) 
        {
            $totalPaid += $this->payout;
            $creditSum -= $this->payout;   
        }
        else // $this->payout > $creditSum
        {
            $totalPaid += $creditSum;
            $creditSum = 0;
        }
        
  
    } // endwhile

    return round($totalPaid,2);
} // calculateCredit

}  // class CreditCalculator

$creditSum = 39999;
$payout = 5000;
$calculator = new CreditCalculator($creditSum, $payout);

$homoCreditRate = 4; // %
$homoComission = 500; // roubles
$homoOPayment = 0; // roubles 
$homoCreditTotal = $calculator->calculateCredit($homoCreditRate, $homoComission, $homoOPayment);

$softbankCreditRate = 3; // %
$softbankComission = 1000; // roubles
$softbankOPayment = 0; // roubles 
$softbankTotal = $calculator->calculateCredit($softbankCreditRate, $softbankComission, $softbankOPayment);

$strawberryCreditRate = 2; // %
$strawberryComission = 0; // roubles
$strawberryOPayment = 7777; // roubles 
$strawberryTotal = $calculator->calculateCredit($strawberryCreditRate, $strawberryComission, $strawberryOPayment);

echo '<pre>';
echo "homoCredit: {$homoCreditTotal} rub. \n";
echo "softbankCredit: {$softbankTotal} rub. \n";
echo "strawberryCredit: {$strawberryTotal} rub. \n";
