Vincent18 Posted March 22, 2008 Share Posted March 22, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/ Share on other sites More sharing options...
Daniel0 Posted March 22, 2008 Share Posted March 22, 2008 In the withdraw(), deposit() and display() methods you need to reference it as $this->balance (same goes for the other ones as well) like you did in the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498462 Share on other sites More sharing options...
Vincent18 Posted March 22, 2008 Author Share Posted March 22, 2008 Thanks a bunch, that did it. I knew it was some syntax thing, just couldn't figure it out. So every reference to a class variable needs $this-> infront of it? Do you need $this-> infront of a method variable? Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498468 Share on other sites More sharing options...
KrisNz Posted March 22, 2008 Share Posted March 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498469 Share on other sites More sharing options...
Daniel0 Posted March 22, 2008 Share Posted March 22, 2008 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498470 Share on other sites More sharing options...
KrisNz Posted March 22, 2008 Share Posted March 22, 2008 well then.....carry on Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498477 Share on other sites More sharing options...
Vincent18 Posted March 22, 2008 Author Share Posted March 22, 2008 Thanks a bunch guys. I'm used to java, so I just have to get the PHP syntax burned into the ol' noggin. Quote Link to comment https://forums.phpfreaks.com/topic/97409-oop-class-stuff-help-out-a-noob/#findComment-498486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.