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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<? 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:
Yup, I really have Apache on port 81 :).
The code is available in GitHub here.
Enjoy it!