<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>	public function setClass($class){
		switch($class){
			case "050":
			case "055":
			case "060":
			case "065":
			case "070":
			case "077":
			case "085":
			case "092":
			case "100":
			case "110":
			case "125":
			case "150":
			case "175":
			case "200":
			case "250":
			case "300":
				$this-&gt;class = $class;
				break;
			default:
				print "invalid class!";
				return false;
		}
		
		return true;
	}</code>
  <comment>I'm making a switch statement to verify some input. It's got all of the possible values (not likely to change much) and if the passed item isn't one of these values it prints an error. Is this a good idea? Is there a better way? Thanks!</comment>
  <created-at type="datetime">2008-08-12T18:19:17+00:00</created-at>
  <id type="integer">431</id>
  <language>PHP</language>
  <permalink>switch-statement-a-good-idea</permalink>
  <refactors-count type="integer">5</refactors-count>
  <title>switch statement a good idea?</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2008-10-09T02:54:21+00:00</updated-at>
  <user-id type="integer">905</user-id>
  <refactors type="array">
    <refactor>
      <code>$CLASSES_CONSTANT = array("050","055","060");

function setClass($class)
{
    if(in_array($class, $CLASSES_CONSTANT))
    {  
      $this-&gt;class = $class;
    }  
    else
    {
      print "invalid class!";
    }
}      </code>
      <code-id type="integer">431</code-id>
      <comment>It's a setter, so there's not really a compelling reason to return anything.  There's a few other fundamental issues with this code, but here's a refactoring to maybe help clean it up and make it easier to add/remove classes in the near future without having to touch the function again.  

Add the $CLASSES_CONSTANT to a global environment file and also rename it if need be, this way you can do the add/remove in 1 spot.</comment>
      <created-at type="datetime">2008-08-12T19:04:21+00:00</created-at>
      <id type="integer">14899</id>
      <language>PHP</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On switch statement a good idea?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Corey Grusden</user-name>
      <user-website>http://blog.divergentsoftware.com</user-website>
    </refactor>
    <refactor>
      <code>public function setClass($class){
	$my_array = array("050", "055", "060", "065", "070", "077", "085", "092", "100", "110", "125", "150", "175", "200", "250", "300");
        if (in_array($class, $my_array)){
		$this-&gt;class = $class;
		break;
	} else {
		print "invalid class!";
		return false;
	}
	return true;
}</code>
      <code-id type="integer">431</code-id>
      <comment>I would use the in_array() function to avoid the long listing and repeated typing:</comment>
      <created-at type="datetime">2008-08-12T19:06:56+00:00</created-at>
      <id type="integer">14900</id>
      <language>PHP</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On switch statement a good idea?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Marco Kranenburg</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">431</code-id>
      <comment>Hrm... not a bad idea. Thanks!</comment>
      <created-at type="datetime">2008-08-12T19:45:46+00:00</created-at>
      <id type="integer">14908</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On switch statement a good idea?</title>
      <user-id type="integer">905</user-id>
      <user-name>cmcculloh</user-name>
      <user-website>http://www.chomperstomp.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">431</code-id>
      <comment>Although I think your code is much faster that using in_array().</comment>
      <created-at type="datetime">2008-09-11T03:00:37+00:00</created-at>
      <id type="integer">16990</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On switch statement a good idea?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Mario Estrada</user-name>
      <user-website>http://mario.ravica.com.ec</user-website>
    </refactor>
    <refactor>
      <code>//somwhere in your class
const CLASSES = {021,078,192,210,240};


//constructor

function __construct($class){
    $this-&gt;setClass($class);
}


function setClass($class){
      if($this-&gt;isValidClass($class)
          $this-&gt;class = $class;
          return true; 
      else
         return false;

}


private function isValidClass($class){
     return in_array($class,SELF::CLASSES);

}</code>
      <code-id type="integer">431</code-id>
      <comment>Well since you are using 'this' you must already be a class so you would have to use a const. Depending on your use case maybe you could even do a check at the constructor to see if it a valid class...here are a few ideas of what I would do to allow the most flexiblity and reusability.</comment>
      <created-at type="datetime">2008-10-09T02:54:20+00:00</created-at>
      <id type="integer">54584</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On switch statement a good idea?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Mark</user-name>
      <user-website></user-website>
    </refactor>
  </refactors>
</code>
