Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/272440-undefined-class/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401724
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401725
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/272440-undefined-class/#findComment-1401980
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.