vipsa Posted December 28, 2012 Share Posted December 28, 2012 Hi I am a complete beginner to php programming and I need to learn it fast as I am not a youngster anymore. I have a book called php and mysql and I am working through it. Creating static member variables I get an error Undefined class constant 'totalDonated' The variable totalDonated is in fact defined in the class definition and here is the code: <?php class Donation { private $name; private $amount; static $totalDonated = 0; static $numberOfDonors = 0; 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() { Donation::$totalDonated = Donation::$totalDonated - $donation; Donation::$numberOfDonars--; } } ?> and here is the code to make use of the class: <?php include "include/donation.inc"; $donors = array( new Donation("Nicholas", 85), new Donation("Thomas", 55), new Donation("James", 78), new Donation("Jeff", 123), new Donation("Sarah", 444) ); foreach($donors as $donor) print $donor->info() . "<br"; $total = Donation::totalDonated; $count = Donation::numberOfDonors; print "Total donations = {$total}<br>"; print "Number of donors = {$count}<br>"; ?> So it makes no sense at all why I would get this error. Please if someone can help so I can learn this language as fast as possible. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/ Share on other sites More sharing options...
priti Posted December 28, 2012 Share Posted December 28, 2012 There are some error I noticed $total = Donation::totalDonated; $count = Donation::numberOfDonors; Must be $total = Donation::$totalDonated; //static variables $count = Donation::$numberOfDonors; //static variables Second In Destructor Donation::$totalDonated = Donation::$totalDonated - $donation; //$donation is not a class property ! Donation::$numberOfDonars--; //variable name is wrong it must be "numberOfDonors" NOT numberOfDonars...check for "a" You fix these thing I think your code will work for sure. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401724 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 Hi vipsa, please use code tags when posting code, and give us the full error message, including line number (with a pointer in the code, if you can, to that line). Incidently, while inside the class, you should use self:: rather than Donation:: unless you've got a reason not to. (I think your book may be a bit dated, i.e. for PHP4) throw a "public" in front of the static as well (it's not needed, but php5 only allows for omitiion for legacy compatability) Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401725 Share on other sites More sharing options...
vipsa Posted December 29, 2012 Author Share Posted December 29, 2012 @priti: I did what you said but I don't understand what you mean by Donation::$totalDonated = Donation::$totalDonated - $donation; //$donation is not a class property ! (this line) and after removing the destruct function it works but only prints one donor instead of 5. Am I missing something? @Muddy_Funster: I will make sure to give more detail and full error description plus put the code inside code tags. I am new at this but I learn fast thx. The book I have says for php5 but I also think it is a bit outdated. Can you recommend a good book? Also is there a website where I can find examples of php projects so I can practise creating them? Thank you for the help. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401980 Share on other sites More sharing options...
Muddy_Funster Posted December 31, 2012 Share Posted December 31, 2012 I personaly can not recomend any up-to-date books on PHP, been a while since I had much time for reading php. If you start a new thread about it though I'm sure there will be severall suggestions, both for books and for projects. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1402305 Share on other sites More sharing options...
cpd Posted December 31, 2012 Share Posted December 31, 2012 Unless you've configured your http server to regonise .inc files it won't interpret it as a PHP page. You currently have include "donation.inc";. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1402322 Share on other sites More sharing options...
Muddy_Funster Posted December 31, 2012 Share Posted December 31, 2012 file extension doesn't matter for includes/requires Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1402324 Share on other sites More sharing options...
vipsa Posted January 3, 2013 Author Share Posted January 3, 2013 Thanks for your comments but it still does not solve my problem as the script does not want to run even after fixing the isues as mentioned in the beginning of this thread. Any help will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1402954 Share on other sites More sharing options...
Muddy_Funster Posted January 3, 2013 Share Posted January 3, 2013 could you let us see your updated code and maybe explain exactly what the problem is now? Quote Link to comment https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1402960 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.