Hi all,
I'm new to this PHP stuff, and relatively new to OOP. I'm trying to do a question for an assignment I have, but the darned thing just wont work. For some reason (i believe) the constructor isn't being called, or variables aren't being passed or stored properly... either way, i'll post my code and if someone could point out the errors in my ways that'd be awesome. The output of this is basically just a lot of empty fields...
class BankAccount
{
//class variables to store information about the bank account
private $firstname, $lastname, $address, $accountNumber, $pinNumber, $balance, $bills;
//class constructor. Used to initialize almost every class variable.
function __construct($fName,$lName,$address,$accountNumber,$pinNumber)
{
$this->firstName = $fName;
$this->lastName = $lName;
$this->address = $address;
$this->accountNumber = $accountNumber;
$this->pinNumber = $pinNumber;
$balance = 0;
$bills = array("billA" => array(
"BC Tel",
"994567"
),
"billB" => array(
"BC Hydro",
"113243"
)
); //initalize some bills in a multidimensional array
}
//Withdraw funds from an account only if the result is not a negative balance
function withdraw($amount)
{
//checks class invariant to make sure that a negative balance cannot be achieved. If all pass, then money is withdrawn
if($amount <= 0) echo "Please do not input a value that is less than or equal to $0.00</br>";
else if($balance <= 0) echo "You currently have no funds in your account, please deposite some</br>";
else if(($balance - $amount) < 0) echo "Insufficient funds. Please withdraw a smaller amount</br>";
else
{
$balance -= $amount;
echo "Funds have been withdrawn. Your current balance is $$balance </br>";
}
}
//add money to an account. Cannot take negative values
function deposit($amount)
{
if($amount >= 0) echo "$$amount added to account. Your current balance is $$balance</br>";
else echo "You can't deposit a negative amount</br>";
}
//Displays account information.
function display()
{
echo "\$balance = $balance </br>
\$firstname = $firstName </br>
\$lastname = $lastName </br>
\$address = $address </br>
\$accountNumber = $accountNumber </br>
\$pinNumber = $pinNumber </br>
\$bills =", var_dump($bills),"<br>";
}
}
$bob = new BankAccount("Bob","Williams","1258 Spooner St","213454","8797");
echo "</br>Bob's initial account information:</br>",$bob->display(),"\n";
echo "Bob trying to withdraw from an empty balance:",$bob->withdraw("500"),"\n";
echo "Bob trying to deposit a negative value: ",$bob->deposit("-100"),"\n";
echo "Bob smartening up and depositing $20: ",$bob->deposit("20"),"\n";
echo "Bob withdrawing $10: ",$bob->withdraw("10"),"\n";
echo "Bob's final account status: </br>",$bob->display(),"\n";