PHP – class with functions

After some time with JS and Python, this week I have decided to continue with PHP. So, let’s see how you can make a class with a function in PHP.

php-elephant

My class would be called Investment, with 3 public variables – interest, amount and result. The function would take two variables and produce value to the third.

Like this:

<?
class Investment{
 public $interest;
 public $amount;
 public $result;
 
 function calculate($interest,$amount){
 $this->result = $this->amount * $this->interest+ $this->amount;
 }
 
}

$investment = new Investment;
$investment->interest = 0.1;
$investment->amount = 250;
$investment->calculate($investment->interest, $investment->amount);

echo "Finally I calculated that from <br>".$investment->amount." 
 if we have ".$investment->interest." we will get ".$investment->result.". <hr>"; 
?>

And this happens if you call it in a browser:

phpClass

 

Yup, I really have Apache on port 81 :).

The code is available in GitHub here.

Enjoy it!