I have created a bank account class with the following variables, the $Balance
variable is private so i use a setter and getter function to access the private property of balance.
class BankAccount
{
public $HoldersName;
public $AccountNumber;
public $SortCode;
private $Balance = 1;
public $APR = 0;
public $Transactions = []; //array defined.
public function __construct($HoldersName, $AccountNumber, $SortCode, $Balance = 1) //constructor where i.e. HoldersName is the same as the variable $HoldersName.
{ //constructor automatically called when object is created.
echo "Holders Name: " . $this->HoldersName = $HoldersName . "";
echo "Account Number: " . $this->AccountNumber = $AccountNumber . "";
echo "Sort Code: " . $this->SortCode = $SortCode . "";
$this->Balance = $Balance >= 1 ? $Balance : 1;
}
public function set_Balance($Balance) //SET balance.
{
return $this->Balance=$Balance;
}
public function get_Balance() //GET balance.
{
return $this->Balance; //Allows us to access the prviate property of $Balance.
}
}
The SavingsAccount
class extends BankAccount
so it inherits everything from the BankAccount
class. I created a function to calculate the interest i.e. the balance 6800 * (0.8+0.25) * 1.
class SavingsAccount extends BankAccount
{
public $APR = 0.8;
public $APRPaymentPreference;
public $ExtraBonus = 0.25;
public function CalculateInterest()
{
$this->Balance = $this->Balance * ($this->APR + $this->ExtraBonus) * 1; //this is where i get the error now
}
public function FinalSavingsReturn()
{
}
}
Here I created an instance of the class SavingsAccount
, with the balance of 6800, i tried to call the function SavingsAccount::CalculateInterest()
but came up with this error:
NOTICE Undefined property: SavingsAccount::$Balance on line number 42
//define objects
$person2 = new SavingsAccount ('Peter Bond', 987654321, '11-11-11', 6800); //create instance of class SavingsAccount
echo "Interest:";
print_r ($person2->CalculateInterest());
No comments:
Post a Comment