Jump to content

Calculating Change Denominations w/ classes


Inked

Recommended Posts

Hi. So I'm trying to learn classes and how to use them with PHP and I'm stuck. I'm trying to make a script that takes input from a user of 2 amounts and stores them into variables. I would then like to use these variables in a couple of functions to make some calculations and finally echo my findings. I'm close but I've still got issues and it seems like I'm now running in circles.

 

I've been reading documentation on classes and googling, but I'm quite stuck. I'm hoping for perhaps some explanations. I could do what I need to do in just another php processing script. But I'm really trying to learn classes and how they can be used with other pages and how general they can be. Here's what I got so far:

 

CalculateChange.php

 

http://pastebin.com/ctdkYjXc#

 

class_Change.php

 

http://pastebin.com/gdfYGXwV#

Well, at a glance you're still strugling with the scope of variables within methods.  Your validatePayed and calculateChange methods are refferencing method specific variables $changeDue, $change, $oneHundred, $fifty etc.  and from your parameter devleration it looks like you're actually meening to refference the class parameters of the same name which would require the same $this-> prefix that you use to get the values of the paramter varialbes on the right of the assignments.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.