Jump to content

$this->subject not working??


spires

Recommended Posts

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 :)

Link to comment
Share on other sites

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;		
}
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;	
}
}

 

Link to comment
Share on other sites

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';
		}

Link to comment
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.