2014-03-18 24 views
2

我有一個try catch聲明與switch聲明裏面。跳過父母功能,如果案件等於'合併'在PHP

我是一種新的PHP,所以請耐心等待。我知道這對PROS來說可能很簡單。

我想達成什麼是跳過頂部的第一個2條if語句如果case is 'merge'

try { 
    if (!make_thumbnail($videoFile, $thumbLocal)) 
     throw new \Exception ($thumbLocal); 
    if (!put_file($thumbLocal, $thumbRemote)) 
     throw new \Exception($thumbRemote); 
    switch($message->priority) { 
     case 'mp4': 
      make_mp4($videoFile, $mp4file); 
      if (!file_exists($mp4file)) 
       throw new \Exception($mp4file); 
      if (!put_file($mp4file, $remotemp4)) 
       throw new \Exception($remotemp4); 
      send_ready_ping($message->ping, $message->id, VIDEO_MP4); 
      make_webm($videoFile,$webmfile); 
      if (!file_exists($webmfile)) 
       throw new \Exception($webmfile); 
      if (!put_file($webmfile, $remotewebm)) 
       throw new \Exception($remotewebm); 
      send_ready_ping($message->ping,$message->id,VIDEO_BOTH); 
      break; 
     case 'merge': 
      make_merged_video($videoFile, $audioFile, $mp4file, $message->options); 
      if (!file_exists($mp4file)) 
       throw new \Exception($mp4file); 
      if (!put_file($mp4file, $remotemp4)) 
       throw new \Exception($remotemp4); 
      send_ready_ping($message->ping, $message->id, VIDEO_MP4); 
      break; 
     default; 
      make_webm($videoFile,$webmfile); 
      if (!file_exists($webmfile)) 
       throw new \Exception($webmfile); 
      if (!put_file($webmfile, $remotewebm)) 
       throw new \Exception($remotewebm); 
      send_ready_ping($message->ping, $message->id, VIDEO_WEBM); 
      make_mp4($videoFile,$mp4file); 
      if (!file_exists($mp4file)) 
       throw new \Exception($mp4file); 
      if (!put_file($mp4file, $remotemp4)) 
       throw new \Exception($remotemp4); 
      send_ready_ping($message->ping,$message->id,VIDEO_BOTH); 
      break; 
    } 
} 
catch (\Exception $e) { 
    echo "Exception: " . $e->getMessage() . "\n"; 
    send_ready_ping($message->ping, $message->id, VIDEO_FAIL); 
} 

我想跳過縮略圖生成每當case等於「合併」

這些是我想跳過的代碼/事件:

if (!make_thumbnail($videoFile,$thumbLocal)) 
     throw new \Exception ($thumbLocal); 
    if (!put_file($thumbLocal, $thumbRemote)) 
     throw new \Exception($thumbRemote); 

我試圖把它放在switch語句裏面,但是沒有工作:

if (case != "merge") { 
    if(!make_thumbnail($videoFile, $thumbLocal)) 
     throw new \Exception ($thumbLocal); 
    if (!put_file($thumbLocal, $thumbRemote)) 
     throw new \Exception($thumbRemote); 
} 

任何想法?

回答

0

您是否需要在case語句中檢查$message->priority

try 
{ 
    if($message->priority != 'merge'){    
     if(!make_thumbnail($videoFile,$thumbLocal)) 
      throw new \Exception ($thumbLocal); 

     if(!put_file($thumbLocal,$thumbRemote)) 
      throw new \Exception($thumbRemote); 
    } 
    switch($message->priority) 
    { 
     case 'mp4': 
     make_mp4($videoFile,$mp4file); 
     if(!file_exists($mp4file)) 
      throw new \Exception($mp4file); 
     if(!put_file($mp4file,$remotemp4)) 
      throw new \Exception($remotemp4); 
     send_ready_ping($message->ping,$message->id,VIDEO_MP4); 

    ... 

} 
3

我可能會重構和重組,但目前我沒有對此的建議。鑑於現有代碼:

if($message->priority != 'merge' && !make_thumbnail($videoFile,$thumbLocal)) 
    throw new \Exception ($thumbLocal); 

if($message->priority != 'merge' && !put_file($thumbLocal,$thumbRemote)) 
    throw new \Exception($thumbRemote); 
+0

好的,謝謝。我忘了'$ message-> priority'是這裏的解決方案。我會試試這個 –

2

你的命名是有點奇怪,你正在做與$message->priority被捆綁到一些什麼樣的行動爲該請求並沒有真正該消息的優先級。但是,你的解決方案應該是容易的,只是一個新的測試包的兩個外if S:

if ($message->priority != "merge") { 
    if(!make_thumbnail($videoFile,$thumbLocal)) 
     throw new \Exception ($thumbLocal); 

    if(!put_file($thumbLocal,$thumbRemote)) 
     throw new \Exception($thumbRemote); 
} 

有你有它,沒有別的需要改變。你的情況可以保持原樣。

+0

謝謝。我忘了我真的在switch語句中傳遞了值。大聲笑。 –

+0

+1我在這種情況下可能會這樣做。 – AbraCadaver

+0

謝謝你們,我正在學習很多:) –