Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with a switch statment

Posted on 2011-02-11
8
Medium Priority
?
187 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 111

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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 …

688 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