PHPマニュアルがバージョンアップ
関数リファレンスに親カテゴリができてた。
PHPマニュアルを眺めてたらPHP5.3、PHP6.0からの内容がいろいろ追加されてた。
名前空間
遅延静的束縛 (Late Static Bindings)
あとエラー例外のページも追加されてた。
ErrorException
前はこっそりあっただけなのに。
PHPの標準関数のエラーを例外エラーとしてthrowするのでtry-catchで楽です。
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
echo "開始\n";
try {
fopen();
strpos();
} catch(Exception $e) {
$e->getMessage() . "\n";
}
echo "終了\n";
# 開始
# fopen() expects at least 2 parameters, 0 given
# 終了
例外といえばタイプヒンティング利用すれば処理もいろいろできます。
<?php
try {
throw new LogicException('例外');
} catch (ErrorException $e) {
echo 'エラー' . $e->getMessage();
} catch (LogicException $e) {
echo 'ロジック' . $e->getMessage(); }
# ロジック例外
カテゴリー: 日記