オブジェクト指向ってこういうこと?(3) エラー処理
これは、自分の中では確信。
エラー処理はtry〜catchで
クラスでのエラーは、基本、exceptionで返してtry〜catchで受ける。
TVChangeChannel.phpで
try { $television = new Television; $command = Remote_Controller::channel1(); $television->remoteControle($command); } catch (Exception $e) { print $e->getMessage(); error_log($e->getMessage()); exit; }
buttonChannel1(), remoteControle()あるいはその中で使ってる関数で、エラー終了する場合は throw Exceptionする。
public function remoteControle($command) { if (!$command) { throw new Exception('コマンドを受信できません'); } (略)
など。
一瞬面倒なようですが、実際やってみると凄く楽。