ララベルの「レスキュー」ヘルパー機能は驚くべきものです
The
try/catchブロックの典型的なユースケースを見てみましょう.
何かのためのコードの醜い作品は本当に簡単です.
救助への救助
rescue
function は、手動でスローされた例外を処理するときに必要な伝統的なtry/catchブロック構文に代わるものです.コールバックを受け取り、tryブロック内で実行します.例外がスローされない場合、コールバックの結果が返されます.try/catchブロックの典型的なユースケースを見てみましょう.
$country = 'USA';
try {
$alpha2 = (new ISO3166)->alpha3($country)['alpha2'];
} catch (\Exception $e) {
$alpha2 = null;
}
何かのためのコードの醜い作品は本当に簡単です.
救助への救助
それでは、私たちの空想の新しいrescue
関数.
$alpha2 = rescue(fn () => (new ISO3166)->alpha3($country)['alpha2']);
ここで起こるのは、コールバックがスローされないときにコールバックが実行されるときです.コールバックから返されるものは、$alpha2
変数.を返します.$alpha2
が割り当てられるnull
.
しかし、あなたが値を割り当てられたくないならばnull
? さて、いくつかのオプションがあります.
まず、別の値を返すことができます.例えば、あなたはfalse
代わりにバック.それでrescue
'sの2番目のパラメータに入ります.
$isFalse = rescue(function () {
throw new Exception;
}, false);
第二に、2番目のパラメータをコールバックとすることができます.ここでは、通常、catchブロックの内部で行うことができます.rescue
を返します.例外をコールバックへの引数として取得します.
rescue(function () {
throw new Exception;
}, function ($e) {
// do exception related stuff
return false;
});
// => false
私は、あなたが2つのコールバックをそれに渡している状況にいるとき、この機能が1トンのユーティリティを加えると思いません.しかし、最初のコールバックで何かをしようとしたい場合、失敗した場合は別の値を取得します.rescue
は便利な小さなヘルパー機能です.
私の意見ではrescue
プログラミングは楽しいものです.彼らは散らばっているSYNTAX - Y部分を取り除き、それが実際に何をしているかに集中するコードのきれいなブロックであなたを残すことができます.それはクリーンコードの縮図です.単純な、再利用可能な、あなたがすることができます/キャッチブロックの重要性を溝にすることができます小さな機能を再利用.
あなたの読書の喜びのために、ここに私が書いた実際の、ライブコーディングサンプルです.
public function getCountryByFormat(string $country, CountryFormat $format): array|null
{
return rescue(fn () => match ($format) {
CountryFormat::NAME => (new ISO3166)->name($country),
CountryFormat::ALPHA_2 => (new ISO3166)->alpha2($country),
CountryFormat::ALPHA_3 => (new ISO3166)->alpha3($country),
CountryFormat::NUMERIC => (new ISO3166)->numeric($country),
}, null, false);
}
NOTE: That third parameter of false
tells the function not to report the exception to the error logger. Since we are kind of expecting this exception, we don't want to report it. Watch out for that potential gotcha.
比較のために、それはtry/catchブロックのように見えるものです.
public function getCountryByFormat(string $country, CountryFormat $format): array|null
{
try {
return match ($format) {
CountryFormat::NAME => (new ISO3166)->name($country),
CountryFormat::ALPHA_2 => (new ISO3166)->alpha2($country),
CountryFormat::ALPHA_3 => (new ISO3166)->alpha3($country),
CountryFormat::NUMERIC => (new ISO3166)->numeric($country),
};
} catch (\Exception $e) {
return null;
}
}
あなたが私に尋ねるならば、かなり甘い改善.おかげでテイラーOtwell!
今知っているrescue
, 私は、あなたが私のような場所の上でそれを使っていると思っています.楽しむ!
Reference
この問題について(ララベルの「レスキュー」ヘルパー機能は驚くべきものです), 我々は、より多くの情報をここで見つけました
https://dev.to/clintwinter/laravels-rescue-helper-function-is-amazing-55aa
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
$alpha2 = rescue(fn () => (new ISO3166)->alpha3($country)['alpha2']);
$isFalse = rescue(function () {
throw new Exception;
}, false);
rescue(function () {
throw new Exception;
}, function ($e) {
// do exception related stuff
return false;
});
// => false
public function getCountryByFormat(string $country, CountryFormat $format): array|null
{
return rescue(fn () => match ($format) {
CountryFormat::NAME => (new ISO3166)->name($country),
CountryFormat::ALPHA_2 => (new ISO3166)->alpha2($country),
CountryFormat::ALPHA_3 => (new ISO3166)->alpha3($country),
CountryFormat::NUMERIC => (new ISO3166)->numeric($country),
}, null, false);
}
NOTE: That third parameter of false
tells the function not to report the exception to the error logger. Since we are kind of expecting this exception, we don't want to report it. Watch out for that potential gotcha.
public function getCountryByFormat(string $country, CountryFormat $format): array|null
{
try {
return match ($format) {
CountryFormat::NAME => (new ISO3166)->name($country),
CountryFormat::ALPHA_2 => (new ISO3166)->alpha2($country),
CountryFormat::ALPHA_3 => (new ISO3166)->alpha3($country),
CountryFormat::NUMERIC => (new ISO3166)->numeric($country),
};
} catch (\Exception $e) {
return null;
}
}
Reference
この問題について(ララベルの「レスキュー」ヘルパー機能は驚くべきものです), 我々は、より多くの情報をここで見つけました https://dev.to/clintwinter/laravels-rescue-helper-function-is-amazing-55aaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol