Jump to content

OOP Class stuff... help out a noob


Vincent18

Recommended Posts

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";

Link to comment
Share on other sites

Having two $$ signs next to each other like that will also cause you problems. You need to escape the first one (like you've done in the display() function)

e.g

echo "\${$amount} added to account. Your current balance is \${$this->balance}</br>"; //note the {} aren't necessary but I like to add them for clarity.

Link to comment
Share on other sites

To refer to something in the object scope you'll have to use the -> operator. Inside an object $this will always hold a reference to the object. It's the exact same concept as doing things like $user->username or something like that. To answer your question: yes.

 

Having two $$ signs next to each other like that will also cause you problems. You need to escape the first one (like you've done in the display() function)

e.g

echo "\${$amount} added to account. Your current balance is \${$this->balance}</br>"; //note the {} aren't necessary but I like to add them for clarity.

It won't be a problem. Variable variables only work inside strings when using complex syntax so the first dollar sign will be interpreted as a literal dollar sign where as the following one will be interpreted as a variable. See this example to demonstrate it:

<?php
$balance = '10';
echo "Balance is $$balance"; // Output: Balance is $10
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.