Solved

problem with a switch statment

Posted on 2011-02-11
8
180 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
ID: 34869936
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
ID: 34869976
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
ID: 34870062
then pleas post the complete class
0
 
LVL 1

Author Comment

by:derrida
ID: 34870103
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:hernst42
ID: 34870246
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
ID: 34870252
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
ID: 34870392
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
ID: 34871967
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

14 Experts available now in Live!

Get 1:1 Help Now