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
https://forums.phpfreaks.com/topic/217183-this-subject-not-working/
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;		
}
}

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?

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

 

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?

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.