私はこれを長い間検索し(ここでも)、多くのphpコードを読みましたが、まだ満足のいく答えを見つけることができませんでした. トピックが広すぎるように思えるかもしれませんが、実際にはまとまりがあります - ついに私にとって。助けていただけますか?
PHP Web サイトでは、BLL オブジェクトで使用される PDO を DAL として使用しており、それらは UI から呼び出されます。何かが起こると、PDO は PDOException をスローします。もちろん、UI レイヤーは PDOExceptions について何も知る必要がないため、BLL オブジェクトがそれをキャッチします。しかし、今は何ですか?
私はそれを読みました
- 例外は本当に例外的な状況のためのものであり、
- 上位層で低レベルの例外を取得しないために、下位層から例外を再スローします。
私の問題を説明しましょう (関数の引数に注意を払わないでください):
class User
{
function signUp()
{
try
{
//executes a PDO query
//returns a code/flag/string hinting the status of the sign up:
//success, username taken, etc.
}
catch (PDOException $e)
{
//take the appropriate measure, e.g. a rollback
//DataAccessException gets all the information (e.g. message, stack
//trace) of PDOException, and maybe adds some other information too
//if not, it is like a "conversion" from PDOException to DAE
throw new DataAccessException();
}
}
}
//and in an upper layer
$user = new User();
try
{
$status = $user->signUp();
//display a message regarding the outcome of the operation
//if it was technically successful
}
catch (DataAccessException $e)
{
//a technical problem occurred
//log it, and display a friendly error message
//no other exception is thrown
}
これは正しい解決策ですか?PDOException を再スローする場合、例外チェーンを使用するのは適切ではないと思います (これはデバッグ情報を冗長にするだけなので、DataAccessException は PDOException からの完全なスタック トレースを含むすべてを取得します)。
前もって感謝します。