unserializeエラーを生成すると、PHPで例外を捕捉する方法はありますか?例外をシリアル化解除する方法は?
12
A
答えて
8
いいえ、キャッチすることはできません。unserialize()
は例外をスローしません。
渡された文字列が直列化不能でない場合、FALSEが返され、E_NOTICEが発行されます。
あなたはすべてのエラーを処理するためのカスタム例外ハンドラを設定することができます。
function exception_error_handler($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
3
は例外にすべてのPHPエラー(警告の通知など)を変換します。例はhereです。
$ret = @unserialize($foo);
if($ret === null){
//Error case
}
しかし、それは最も近代的なソリューションではありません。
9
簡単な方法があります。
上記のように、カスタムエラー/例外ハンドラ(この場合のみならず)を使用するのが最善の方法です。しかし、あなたがやっていることに応じて、それは過度のことかもしれません。
2
完全なソリューションには、次のようになります。ドキュメントあたり
<?php
// As mentioned in the top answer, we need to set up
// some general error handling
function exception_error_handler($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
// Note, there are two types of way it could fail,
// the fail2 fail is when try to unserialise just
// false, it should fail. Also note, what you
// do when something fails is up to your app.
// So replace var_dump("fail...") with your
// own app logic for error handling
function unserializeSensible($value) {
$caught = false;
try {
$unserialised = unserialize($value);
} catch(ErrorException $e) {
var_dump("fail");
$caught = true;
}
// PHP doesn't have a try .. else block like Python
if(!$caught) {
if($unserialised === false && $value !== serialize(false)) {
var_dump("fail2");
} else {
var_dump("pass");
return $unserialised;
}
}
}
unserializeSensible('b:0;'); // Should pass
unserializeSensible('b:1;'); // Should pass
unserializeSensible('a:2:{s:1:"a";b:0;s:1:"b";s:3:"foo";}'); // Should pass
unserializeSensible('a:2:{s:1:"a";b:0;s:1:"b";s:3:1111111111111111;}'); // Should fail
unserializeSensible(123); // Should fail
unserializeSensible("Gday"); // Should fail
unserializeSensible(false); // Should fail2
unserializeSensible(true); // Should fail
関連する問題
- 1. 例外のシリアル化方法
- 2. シリアル化中にObjectMapperのフィールドを除外する方法は?
- 3. 特定の型をシリアル化から除外する方法は?
- 4. .Net XMLのシリアル化の例外を処理する方法?
- 5. DataContractSerializerシリアル化のスロー例外#
- 6. WCF CFClientBaseシリアル化Stackoverflow例外?
- 7. Javaシリアル化のEOF例外
- 8. WCFシリアル化例外 - NetDataContractSerializer
- 9. 配列のシリアル化を解除する
- 10. データベースセッションのシリアル化を解除するCodeigniter
- 11. データのシリアル化解除
- 12. Protobuf-Netを使用して.Net例外をシリアル化する方法は?
- 13. シリアル化の例外は声明
- 14. データベースデータをシリアル化/シリアル化解除しますか?
- 15. PHPで大きなファイルをシリアライズしてシリアル化を解除する方法は?
- 16. ddbgを使用して例外を解除する方法
- 17. JSONを解析してnullpointer例外を排除する方法
- 18. 投稿されたデータのシリアル化を解除する方法は?
- 19. Jacksonのシリアル化から1つのコレクション要素を除外する方法は?
- 20. jsonpickleでシリアル化の特定のフィールドを除外する方法は?
- 21. VS2015でカスタム例外を解除する方法
- 22. MongoDBオブジェクトのシリアル化、スタックオーバーフロー例外
- 23. GWT - RPCのシリアル化例外とMapDB
- 24. jQuery - シリアル化された文字列のシリアル化を解除する
- 25. ランタイム中にJSON/XMLシリアル化からデータメンバーを除外する方法
- 26. Json.NET - クラスへのアクセスなしでシリアル化からプロパティを除外する方法
- 27. PDOException:PDOインスタンスをシリアル化またはシリアル化解除できません
- 28. GenericXmlSecurityTokenを格納/逆シリアル化/シリアル化する方法は?
- 29. Pythonで例外を "解除"する方法はありますか?
- 30. PublisherInterstitialAdオブジェクトのシリアライズとシリアル化解除
:渡された文字列はunserializeableでない場合は、FALSEが返されます。幸運にも、 'serialize(false)'を行う人は幸いです。 – gfaceless
"渡された文字列が直列化不能でない場合、FALSEが返され、** E_NOTICEが発行されます。**" E_もスローされます。 – zedee