Jump to content

Classes with set_error_handler


CarbonCopy

Recommended Posts

I am designing an application which I want to have custom error reporting, and I am using a class. However, when using self:: I get this error:

 

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /www/.............../error.inc.php on line 38 

 

And here is error.inc.php

 

<?php
/**
  * @author  Brandon Wamboldt
  * @package northern-cms
  * @version 1.0.0
  * @date    November 1st, 2009
  *
  * This file is the intelluctual property of Northern Lights Technology. Any
  * unauthorized use, modifications, or distribution of this code is prohibted.
  *
  * http://northernlightstechnology.ca/legal
  */
  
class error {

/*
 * Object - Pointer to the root class
 */
public static $cms;

/**
 * Required initialization class
 */
public static function init()
{
	set_error_handler( array( "error" , "error_handler" ) );
}

/**
 * @name show_fatal_error
 * @desc Changes the logging directory
 * @vars dir - String - The path to the new directory
 * @return Boolean - True if the logging directory was changed, false if there is a problem with it
 */
public static function error_handler( $errno , $errstr , $errfile , $errline )
{
	/** Based on debugging mode send output **/
	if ( self::cms->database->connected )
	{
		if ( self::cms->database->get_setting( 'debug_mode' ) == 1 )
		{
			if ( $errno === E_USER_ERROR )
			{
				self::show_fatal_error();
			}
		}
		else if ( self::cms->database->get_setting( 'debug_mode' ) == 2 )
		{
			if ( $errno === E_USER_ERROR )
			{
				echo "<b>Northern CMS:</b> [$errno] $errstr<br />\n";
				echo "  Fatal error on line $errline in file $errfile";
				echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
				echo "Aborting...<br />\n";
				self::show_fatal_error();
			}
		}
		else if ( self::cms->database->get_setting( 'debug_mode' ) == 3 )
		{
			if ( $errno === E_USER_ERROR )
			{
				echo "<b>Northern CMS:</b> [$errno] $errstr<br />\n";
				echo "  Fatal error on line $errline in file $errfile";
				echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
				echo "Aborting...<br />\n";
				self::show_fatal_error();
			}
			else if ( $errno == E_USER_WARNING )
			{
				echo "<b>Northern CMS:</b> [$errno] $errstr<br />\n";
			}
		}
		else if ( self::cms->database->get_setting( 'debug_mode' ) == 4 )
		{
			if ( $errno === E_USER_ERROR )
			{
				echo "<b>Northern CMS:</b> [$errno] $errstr<br />\n";
				echo "  Fatal error on line $errline in file $errfile";
				echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
				echo "Aborting...<br />\n";
				self::show_fatal_error();
			}
			else if ( $errno == E_USER_WARNING )
			{
				echo "<b>Northern CMS Warning:</b> [$errno] $errstr<br />\n";
			}
			else if ( $errno == E_USER_NOTICE )
			{
				echo "<b>Northern CMS Notice:</b> [$errno] $errstr<br />\n";
			}
		}
		else
		{
			if ( $die )
			{
				exit();	
			}
		}
	}
	else
	{
		if ( $die )
		{
			self::show_fatal_error();
			exit();	
		}
	}
}

/**
 * @name show_fatal_error
 * @desc Changes the logging directory
 * @vars dir - String - The path to the new directory
 * @return Boolean - True if the logging directory was changed, false if there is a problem with it
 */
public static function show_fatal_error()
{
	echo '<h1 style="font-style:italic;">Oops, we\'ve encounted an error</h1>';
	echo '<p style="font-weight:bold;font-size:12px;width:400px;">';
	echo 'This program has encounted a fatal error and was unable to continue. Please refresh your page, or if the ';
	echo 'problem persists, contact the administrator of this website';
	echo '</p>';
	exit();	
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/180292-classes-with-set_error_handler/
Share on other sites

When using the self keyword in combination with a class property, the property does need to be prefixed with a dollar sign.

 

ex:

// Old (ERROR)
if ( self::cms->database->connected )

// New (No Error)
if ( self::$cms->database->connected )

I see this error on lines

38

40

47

58

73

Well that fixed that problem, so thank you. Now I get an error for the class calling that method.

 

[04-Nov-2009 21:47:04] PHP Parse error:  syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /www/.............../test.php on line 17

 

and test.php

 

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');

class cms {
function init() {
	include('error.inc.php');
	$this->error = new error();
	$this->error::$cms = &$this;
	#$this->error::init();
	echo "HEY";exit();

	include('log.inc.php');
	$this->logger = new logger();
	$this->logger->cms = &$this;
	$this->logger->init('../logs/');

	include('database.inc.php');
	$this->database = new database();
	$this->database->cms = &$this;
	$this->database->init('localhost','dev_cms','root','**********','mysql');
	echo $this->database->get_setting('base_path');
}
}

$cms = new cms();

$cms->init();
?>

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.