...

View Full Version : Instantiating a class



ScottInTexas
08-24-2007, 06:30 PM
I created a class in my file 'functions.php'. In 'index.php' I include functions and later in the file I have this code;


Case 'mailit':
$thismsg=new mailme;
$mailme->setTo($_POST['toadd']);
$mailme->setFrom($_POST['fromadd']);
$mailme->setText($_POST['mailmsg']);
$mailme->setSubject($_POST['mailsub']);
$mailme->sendMail;
include("mailsent.php");
break;


Im getting this error:
Fatal error: Call to a member function on a non-object in c:\phpdev5\www\alaskaerrands\index.php on line 131

My class which is borrowed from several sources, looks like this



class mailme{

//---Global Variables
var $mailTo= ""; var $mailFrom= ""; var $mailSubject= ""; var $mailText= "";

function setTo($inAddress){

$this->mailTo = $inAddress;
return true;
}



I removed the other functions for clarity.

I just read an article on using classes and I gather I am doing it right, but alas, there is an error so...

Thanks.

Mwnciau
08-24-2007, 06:50 PM
Case 'mailit':
$thismsg=new mailme;
$mailme->setTo($_POST['toadd']);
$mailme->setFrom($_POST['fromadd']);
$mailme->setText($_POST['mailmsg']);
$mailme->setSubject($_POST['mailsub']);
$mailme->sendMail;
include("mailsent.php");
break;

The variable with the class is $thismsg not $mailme

ScottInTexas
08-24-2007, 08:35 PM
How utterly stupid of me! It was so obvious I'm surprised I didn't get whacked up side my head!

ScottInTexas
08-24-2007, 08:37 PM
How do I call a function within a class. In my case what is the syntax for calling the sendMail function? The way I have it now throws an error.

Notice: Undefined property: sendMail in c:\phpdev5\www\alaskaerrands\index.php on line 135

Inigoesdr
08-24-2007, 08:48 PM
$this->function(args);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum