vipsa Posted October 13, 2011 Share Posted October 13, 2011 I am brand new to php and the following code gives me an error of undefined variable although it seems to be defined. Please tell what I'm doing wrong cause this code comes from a book: <?php // Definition of the class Donation class Donation { private $name; private $amount; static $totalDonated = 0; static $numberOfDonors = 0; // Function to display what each person has donated function info() { $share = 100 * $this->amount / Donation::$totalDonated; return "{$this->name} donated {$this->amount} ({$share}%)"; } function __construct($nameOfDonor, $donation) { $this->name = $nameOfDonor; $this->amount = $donation; Donation::$totalDonated = Donation::$totalDonated + $donation; Donation::$numberOfDonors++; } function __destruct() { 109) Donation::$totalDonated = Donation::$totalDonated - $donation; Donation::$numberOfDonors--; } } It gives me the following error: Notice: Undefined variable: donation in C:\wamp\www\learnPHP\unitCounter.inc on line 109 Quote Link to comment https://forums.phpfreaks.com/topic/249061-newbie-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2011 Share Posted October 13, 2011 Since the constructor stores the $donation into $this->amount, I will guess that your destructor should use $this->amount? The $donation variable only existed as a parameter/local variable in the __construct method/function and does not exist inside the __destruct method/function. Quote Link to comment https://forums.phpfreaks.com/topic/249061-newbie-help/#findComment-1279096 Share on other sites More sharing options...
vipsa Posted October 13, 2011 Author Share Posted October 13, 2011 Thank you for the help. I gathered that much but I can't understand why they had it like that in the book. So the __destruct function should not be there at all or how do I fix this? Also the newline character is not printing why would that be? Quote Link to comment https://forums.phpfreaks.com/topic/249061-newbie-help/#findComment-1279100 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.