Solved

problem with a switch statment

Posted on 2011-02-11
8
179 Views
Last Modified: 2012-05-11
Hi
i`m writing my own error handler class. i want to display different message and do different actions according to the error level and according to the user level (user or admin).

so i loop an array of levels and then do a switch according to the error level and an inner switch according to the user level.

but when i try to test it with:
$tuch =  trigger_error('this is a user error', E_USER_EREROR);

i get all levels messages:
his is Warning for admin - Warning
this is Notice for admin - Notice
this is User Error for admin - User Error
this is User Warning for admin - User Warning
this is User Notice for admin - User Notice
this is Warning for admin - Warning
this is Notice for admin - Notice
this is User Error for admin - User Error
this is User Warning for admin - User Warning
this is User Notice for admin - User Notice

obviously i want to get (in this case) just the user error.

i attach the code.

best regards
foreach ( $this ->types_array as $key => $value )
                              {
                                             $thelevel =  $value;
                                             
                                             switch ( $thelevel )
                                                            {
                                                                           case 'Warning':
                                                                                          
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                       print "this is Warning for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                        print "this is Warning for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }


                                                                                          break ;
                                                                            case 'Notice':
                                                                                           switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                   print "this is Notice for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                        print "this is Notice for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;
                                                                             case 'User Error':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:

                                                                                                                       print "this is User Error for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                    print "this is User Error for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;
                                                                             case 'User Warning':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:

                                                                                                                       print "this is User Warning for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                       print "this is User Warning for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;

                                                                             case 'User Notice':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                       print "this is User Notice for users - $thelevel<br/>";
                                                                                                                        break ;
                                                                                                                      

                                                                                                         case 1:
                                                                                                                       print "this is User Notice for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;

                                                            }
                              }

Open in new window

0
Comment
Question by:derrida
8 Comments
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
Problem is:
foreach ( $this ->types_array as $key => $value )
   $thelevel =  $value;
   switch ( $thelevel )

Same as
foreach ( $this ->types_array as $key => $thelevel )
   switch ( $thelevel )


Guess if you remove the line
$thelevel =  $value;
It should work if you have the Parameter $thelevel in your method declaration
0
 
LVL 1

Author Comment

by:derrida
Comment Utility
hi
i made this in the class properties:
private $thelevel;

and the in the method:
 foreach ( $this ->types_array as $key => $value )
                              {
                                            // $this->thelevel =  $value;
                                             
                                             switch ( $this ->thelevel )
                                                            {

but then obviously $this->thelevel has no value.
0
 
LVL 48

Expert Comment

by:hernst42
Comment Utility
then pleas post the complete class
0
 
LVL 1

Author Comment

by:derrida
Comment Utility
hi

well it is just the starting point but i attach the whole class.
<?php
     date_default_timezone_set ( 'Asia/Tel_Aviv' ) ;

class er {
    //properties here
    private $types_array;
    private $debug = 0;
    private $error_date ;
    private $thelevel;


    public function __construct($debug = 0)
               {
                                  $this -> debug = $debug ;
                                  set_error_handler ( array ( $this , 'handleError' ) ) ;
                                  $this -> error_date = date ( "d/m/Y H:i:s (T)" ) ;
               }

               public function handleError( $errno , $errmsg , $filename , $linenum , $vars)
               {
                                 
                                  // mostly we will use: E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING and E_USER_NOTICE
                              $this ->types_array = array (
                                          E_ERROR => 'Error' ,
                                          E_WARNING => 'Warning' ,
                                          E_NOTICE => 'Notice' ,
                                          E_USER_ERROR => 'User Error' ,
                                          E_USER_WARNING => 'User Warning' ,
                                          E_USER_NOTICE => 'User Notice' 
                                  ) ;


                              foreach ( $this ->types_array as $key => $value )
                              {
                                           $this->thelevel =  $value;
                                             
                                             switch ( $this ->thelevel )
                                                            {
                                                                           case 'Warning':
                                                                                          
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                       print "this is Warning for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                        print "this is Warning for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }


                                                                                          break ;
                                                                            case 'Notice':
                                                                                           switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                   print "this is Notice for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                        print "this is Notice for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;
                                                                             case 'User Error':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:

                                                                                                                       print "this is User Error for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                    print "this is User Error for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;
                                                                             case 'User Warning':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:

                                                                                                                       print "this is User Warning for users - $thelevel<br/>";
                                                                                                                        break ;

                                                                                                         case 1:
                                                                                                                       print "this is User Warning for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;

                                                                             case 'User Notice':
                                                                                          switch ( $this ->debug)
                                                                                          {
                                                                                                         case 0:
                                                                                                                       print "this is User Notice for users - $thelevel<br/>";
                                                                                                                        break ;
                                                                                                                      

                                                                                                         case 1:
                                                                                                                       print "this is User Notice for admin - $thelevel<br/>";
                                                                                                                       break ;
                                                                                          }
                                                                                          break ;

                                                            }
                              }

                              

                             

               }


            
}//end of class

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 48

Expert Comment

by:hernst42
Comment Utility
ok. In this case I would write it as:
class er
{

    //properties here
    private $types_array = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_NOTICE => 'Notice', E_USER_ERROR => 'User Error', 
                        E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice');

    private $debug = 0;

    private $error_date;

    private $thelevel;

    public function __construct($debug = 0)
    {
        $this->debug = $debug;
        set_error_handler(array($this, 'handleError'));
        $this->error_date = date("d/m/Y H:i:s (T)");
    }

    public function handleError($errno, $errmsg, $filename, $linenum, $vars)
    {
        if (isset($this->types_array[$errno])) {
            return true;
        }
        $this->thelevel = $this->types_array[$errno];
        
        foreach ($this->types_array as $key => $value) {
        
        switch ($this->thelevel) {
....

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please add error_reporting(E_ALL) to your scripts when you are trying to debug them.  Errors of the type you have described here are usually data-dependent and you will want to be absolutely certain that all your variables are properly initialized in scope or namespace before you use them.

You might try simplifying the code a little bit.  You do not need to use nested switch/case statements.  Similarly, I do not understand what value you gain by creating the types_array.  I think I would go back and try to write out my goals in plain language before I started coding or recoding this.
<?php
  date_default_timezone_set ( 'Asia/Tel_Aviv' ) ;

class er 
{
    private $types_array;
    private $debug = 0;
    private $error_date ;
    private $thelevel;

    public function __construct($debug=0)
    {
        $this ->debug = $debug ;
        set_error_handler ( array ( $this , 'handleError' ) ) ;
        $this ->error_date = date ( "d/m/Y H:i:s (T)" ) ;
        $this ->types_array = array 
        ( E_ERROR        => 'Error'
        , E_WARNING      => 'Warning' 
	, E_NOTICE       => 'Notice' 
	, E_USER_ERROR   => 'User Error' 
	, E_USER_WARNING => 'User Warning' 
	, E_USER_NOTICE  => 'User Notice' 
	)
	;
    }

    public function handleError($errno, $errmsg, $filename, $linenum, $vars)
    {
        foreach ($this ->types_array as $key => $value)
        {
            $this->thelevel =  $value;
            switch ( $this->thelevel )
            {
                case 'Warning':
                if ($this->debug)
                {
                    print "this is Warning for admin - $thelevel<br/>";
                }
                else
                {
                    print "this is Warning for users - $thelevel<br/>";
                }
                break;
            }
        }
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:derrida
Comment Utility
Hi

ernset - it still give me the same result.

ray - when i trigger this:
$tuch =  trigger_error('this is a user error', E_WARNING);

i get all the messages for all levels and not just for E_WARNING.

i do have error_reporting on the page that test the class.

0
 
LVL 2

Accepted Solution

by:
requeue earned 500 total points
Comment Utility
I guess that you want to show admin errors only during debug.
If true, you don't need for each.

My solution is like this.

<?php
class error {
	public function __construct()
	{
	     set_error_handler(array ( $this , 'handleError' ) );
    }
   
    public function handleError($errno, $errstr, $errfile, $errline, $errcontext)
    {
		$types_array = array (
				E_ERROR => 'Error' ,
				E_WARNING => 'Warning' ,
				E_NOTICE => 'Notice' ,
				E_USER_ERROR => 'User Error' ,
				E_USER_WARNING => 'User Warning' ,
				E_USER_NOTICE => 'User Notice' 
			) ;
		$error_date = date ( "d/m/Y H:i:s (T)" ) ;

		$thelevel = $types_array[$errno];
		switch ( $errno )
		{
			// error for admin
			case E_USER_ERROR:
			case E_USER_WARNING:
			case E_USER_NOTICE:
				// print error 
				print "$error_date: this is $thelevel for users<br/>";
			default:
				break;
		}

		if (DEBUG)
		{
			// error for admin
			switch ( $errno )
			{
				case E_WARNING:
				case E_NOTICE:
					print "$error_date: this is $thelevel for admin<br/>";
					break ;
				default:
					break;
			}
		}
    }
}

// set true during debug
define('DEBUG', true);
$er = new error();

$tuch =  trigger_error('this is a user error',  E_USER_ERROR);

// php will die after handled
$tuch =  trigger_error('this is a admin error',  E_WARNING);
?>

Open in new window


output:
12/02/2011 00:21:06 (JST): this is User Error for users
12/02/2011 00:21:06 (JST): this is Warning for admin

Depending on your purpose, you might not need class
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now