存档在 2009年4月27日

php的多分支控制

2009年4月27日

php的多分支控制
原文 (飞扬轻狂,fallseir) 20090427
==== php的多分支控制 ====
### if 方式 实现多分支控制

if(runStep(1)){
  if(runStep(2)){
    if(runStep(3)){
      # all steps are success!!
    }else{
	# step 03 is failed!
    }
  }else{
    # step 03 is failed!
  }
}else{
  # step 03 is failed!
}

# some others steps

### if 方式 2 实现多分支控制

$r=0;
if(runStep(1))
  if(runStep(2))
    if(runStep(3))
    else $r=3;
  else $r=2;
else $r=1;
if($r){
  # step $r is failed!
  return false;
}

# some others steps

### do..while(false) 方式 2 实现多分支控制

do{
  if(!runStep(1)) # step 01 is failed!
  break;
  if(!runStep(2)) # step 02 is failed!
  break;
  if(!runStep(3)) # step 03 is failed!
  break;
  # all steps are success!!
}while(false);

# some others steps

### switch(true) 方式 2 实现多分支控制

switch(true){
  case !runStep(1):
	# step 01 is failed!
	break;
  case !runStep(2):
	# step 02 is failed!
	break;
  case !runStep(3):
	# step 03 is failed!
	break;
  default:
	# all steps are success!!
    break;
}

# some others steps

### try..catch 方式实现

try{
  if(!runStep(1)) throw new SomeError(1); # step 01 is failed!
  if(!runStep(2)) throw new SomeError(2); # step 02 is failed!
  if(!runStep(3)) throw new SomeError(3); # step 01 is failed!
  # all steps are success!!
}catch(SomeError $ex){
  # step $ex is failed!
  return false;
}
# some others steps

使用反射 实现的 简单的 单元测试框架

2009年4月27日

使用反射 实现的 简单的 单元测试框架
原文 (飞扬轻狂,fallseir) 20090427
==== generalized test methods use introspection ====

class customClass{
  function selfTest(){[..] return [message|false]}
}
class tester{
  function test($thing){
    if(is_object($thing)){
      if(method_exists($thing,'selfTest')){
        $this->handleTest(call_user_method('selfTest',$thing));
      }
    }else if(is_array($thing)){
      foreach($thing as $component){ $this->test($component); }
    }else{
    ;//ignore if not an array or object
    }
  }
  function handleTest($result){if($result)print "Warning $result";}
}

参考:
John.Wiley.and.Sons.PHP5.and.MySQL.Bible