Solved

problem with a switch statment

Posted on 2011-02-11
8
186 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

622 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