Solved

problem with a switch statment

Posted on 2011-02-11
8
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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