spires Posted October 29, 2010 Share Posted October 29, 2010 Hi Can anyone see why $this->subject is not working? I have a class that is going to send an email. All the variables are set and contain values. How ever, I'm getting an error if I try to use the subject variable inside a method? Error: Fatal error: Using $this when not in object context in /home/p/o/powtest/web/public_html/includes/admin_emails.php on line 36 Line 36 is: $subject = $this->subject; protected static $table_name="admin_emails"; protected static $db_fields = array('id', 'name', 'subject', 'body_content', 'from_name', 'from_email'); public $id; public $name; public $subject; public $body_content; public $from_name; public $from_email; public function send_notification($sendTo, $name){ $to_name = $name; $to = $sendTo; $subject = $this->subject; $message =<<<EMAILBODY {$this->body} EMAILBODY; $message = wordwrap($message, 70); $from_name = $this->from; $from = $this->from_email; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "localhost"; $mail->Port = 25; $mail->SMTPAuth = false; $mail->FromName = $from_name; $mail->From = $from; $mail->AddAddress($to, $to_name); $mail->Subject = $subject; $mail->Body = $message; $result = $mail->Send(); return $result; } Any help or pointer would be great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/ Share on other sites More sharing options...
sharal Posted October 29, 2010 Share Posted October 29, 2010 Do you actually have a class wrapping around that? As the error indicates you are using the $this operator outside of the objects scope. Can you try to post the whole class with the class declaration too? Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127913 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 Hi Yes I do. I've got a solution: $new_email = new Admin_emails(); $subject = $new_email->subject; But i'm not sure why the first way is not working. It should work fine. Unless i'm missing something. Full Class: <?PHP require_once(LIB_PATH.DS.'database.php'); class Admin_emails { protected static $table_name="admin_emails"; protected static $db_fields = array('id', 'name', 'subject', 'body_content', 'from_name', 'from_email'); public $id; public $name; public $subject; public $body_content; public $from_name; public $from_email; public static function make($NAME, $SUB, $BODY, $FROM, $Femail){ if(!empty($SUB) && !empty($BODY) && !empty($FROM)){ $kw = new Admin_emails(); $kw->name = $NAME; $kw->subject = $SUB; $kw->body_content = $BODY; $kw->from_name = $FROM; $kw->from_email = $Femail; return $kw; }else{ return false; } } public function send_notification($sendTo, $name){ $new_email = new Admin_emails(); $to_name = $name; $to = $sendTo; $subject = $new_email->subject; $message =<<<EMAILBODY {$new_email->body_content} EMAILBODY; $message = wordwrap($message, 70); $from_name = $new_email->from_name; $from = $new_email->from_email; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "localhost"; $mail->Port = 25; $mail->SMTPAuth = false; $mail->FromName = $from_name; $mail->From = $from; $mail->AddAddress($to, $to_name); $mail->Subject = $subject; $mail->Body = $message; $result = $mail->Send(); return $result; } } Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127914 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 Actually, Scrap that. It's not working with my work around. Yes the error has gone, but the values are now empty? $new_email = new Admin_emails(); $subject = $new_email->subject; echo $subject; $subject is empty I should be able to use $this->subject, but can't? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127918 Share on other sites More sharing options...
sharal Posted October 29, 2010 Share Posted October 29, 2010 it has something to do with your static method probably, I dont usually work with those so afraid I can't help you. My best guess is that it's not possible to access non static class variables(as those are different from each instance of the object) from within the static method, you can however, always access static variables from inside the static method. Class someclass { private static $test = 'hello'; public static function something () { echo self::$test; } } $test = new someClass; $test::something(); Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127919 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 ok, Thanks for your help I'll have a look around a few sites, see if I can find anything Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127922 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 OK. I have taken it right back to bear bones. And still get the error. FULL CLASS class Admin_emails { protected static $table_name="admin_emails"; protected static $db_fields = array('id', 'name', 'subject', 'body_content', 'from_name', 'from_email'); public $id; public $name; public $subject; public $body_content; public $from_name; public $from_email; public function send_notification($sendTo, $name){ $subject = $this->subject; } } What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127924 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 Even more stripped away: Still getting the error: Fatal error: Using $this when not in object context in /home/p/o/powtest/web/public_html/includes/admin_emails.php on line 10 class Admin_emails { public $subject; public function send_notification(){ $sub = $this->subject; } } Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127925 Share on other sites More sharing options...
sharal Posted October 29, 2010 Share Posted October 29, 2010 Strange, I pasted your code directly into my editor and ran it - with no problems whatsoever. Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127928 Share on other sites More sharing options...
spires Posted October 29, 2010 Author Share Posted October 29, 2010 Sorry, Me being a total idiot. I was running: if(Admin_emails::send_notification($sendTo, $toName){ echo 'Saved'; } Instead of: $new_email_obj = new Admin_emails(); if($new_email_obj->send_notification($sendTo, $toName)){ echo 'Saved'; } Quote Link to comment https://forums.phpfreaks.com/topic/217183-this-subject-not-working/#findComment-1127929 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.