0

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 . "</br>";
        echo "Account Number: " . $this->AccountNumber = $AccountNumber . "</br>";
        echo "Sort Code: " . $this->SortCode = $SortCode . "</br>";
        $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 "<b>Interest:</b>";
print_r ($person2->CalculateInterest());
miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

1

As you said, the $Balance property is private. Private means it is not accessible from outside the defining class, not even from child classes that inherit the property.

<?php
class Foo {
    private $bar="baz";
    public function __construct(){echo $this->bar;}
    public function getBar(){return $this->bar;}
}
class Bar extends Foo {
    public function __construct(){echo $this->bar;}
}
new Foo;
new Bar;

If we run this code we get the error:

baz
PHP Notice:  Undefined property: Bar::$bar in php shell code on line 2

So, you have two choices. You can use your getter instead:

<?php
class Foo {
    private $bar="baz";
    public function __construct(){echo $this->bar;}
    public function getBar(){return $this->bar;}
}
class Bar extends Foo {
    public function __construct(){echo $this->getBar();}
}
new Foo;
new Bar;

Or, you can declare the property as protected instead of private:

<?php
class Foo {
    protected $bar="baz";
    public function __construct(){echo $this->bar;}
    public function getBar(){return $this->bar;}
}
class Bar extends Foo {
    public function __construct(){echo $this->bar;}
}
new Foo;
new Bar;

Either of these give the expected output:

baz
baz

See documentation here for more details on property and method visibility.

miken32
  • 42,008
  • 16
  • 111
  • 154