Jump to content

Using $this when not in object context


sinners

Recommended Posts

Hey all, New here.

 

I have been doing php for a couple of weeks now and i am trying to create some code that handles errors. It will eventually store every error that is experienced store them for that day and them send them all in one email. At the moment i have having trouble i keep getting the error: Using $this when not in object context. When i did a search on google i found that it was either due to something about a static variable or my php version. I am using php 5

 

here is the code

 

<?php

class ErrorReporting { 

	var $n = 1;

	 function CustomErrorReport($e_number, $e_message, $e_file, $e_line, $e_vars) {

		if (LIVE) {

			$message["Error occured in script: $e_file on line: $e_line, $e_message"] = $this->n;
			echo $this->n;
			$this->n++;
		}

		else {
			echo 'An Internal Error has occured';
		}

		}	


}



set_error_handler(array('ErrorReporting','CustomErrorReport'));	
foreach ($var as $v) {}
$result=1/0;

?>

 

Obviously the last two lines are to create errors

 

Thanks

 

Sinners

Link to comment
https://forums.phpfreaks.com/topic/125752-using-this-when-not-in-object-context/
Share on other sites

The problem exists because you're telling the php error handler that you want it to use the function "CustomErrorReport" from the class "ErrorReporting" in a static context. i.e. the error handler says "i've got an error, send it to ErrorReporting::CustomErrorReport() ".

 

Because it's calling the function in a static context, you don't have an instance of your ErrorReporting class. Thus trying to use $this inside it will fail.

To avoid this situation make your error_reporting class a singleton, and have an initialisation inside your static function.

 

i've included a prototype for you.

<?php
/**
* Error reporting class.
*
*/
class ErrorReporting { 

/**
 * Internal counter.
 */
private $n;

/**
 * Reference to the instance of self.
 */
static private $_instance = null;

/**
 * Constructor - used to initialise internals.
 * @final
 */
final private function __construct(){
	// Initialise internal parameters.
	$this->n = 1;
}

/**
 * getInstance - returns singleton of self.
 * @static
 */
static public function getInstance(){
	if(null === self::$_instance){
		self::$_instance = new self();
	}
	return self::$_instance;
}

public function setN($n){
	$this->n = $n;
}

public function getN(){
	return $this->n;
}

/**
 * Custom Error reporting function.
 * @static
 */
static public function CustomErrorReport($e_number, $e_message, $e_file, $e_line, $e_vars) {
	$instance = self::getInstance();
	if (LIVE) {
		$message["Error occured in script: $e_file on line: $e_line, $e_message"] = $instance->getN();
		echo $instance->getN();
		$instance->setN($this->getN()+1);
	} else {
		echo 'An Internal Error has occured';
	}

}	
}

set_error_handler(array('ErrorReporting','CustomErrorReport'));	
foreach ($var as $v) {}
$result=1/0;

?>

 

It might be worth noting that you will never get "n" past 1, because once an error is caught PHP will stop.

Just updating my previous post, there is a better way to resolve this problem (instead of using singleton, although singleton isn't bad per se). Just do this:

<?php 
set_error_handler(array(new ErrorReporting(),'CustomErrorReport')); 
?>

 

So the above creates an instance of the class, and tells the error handler to use the CustomErrorReport() method from inside it.

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.