我有一个try catch
声明 switch
里面有声明。
我是 PHP 新手,所以请耐心等待。我知道这对于专业人士来说可能很简单。
我想要实现的是跳过前 2 if
顶部的语句 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);
}
有什么想法吗?
最佳答案
我可能会重构和重组,但目前我没有这方面的建议。鉴于现有代码:
if($message->priority != 'merge' && !make_thumbnail($videoFile,$thumbLocal))
throw new \Exception ($thumbLocal);
if($message->priority != 'merge' && !put_file($thumbLocal,$thumbRemote))
throw new \Exception($thumbRemote);
关于php - PHP 中如果 case 等于 'merge' 则跳过父函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487157/