98

クラスの1つにPHP5の型ヒントを実装しようとしています。

class ClassA {
    public function method_a (ClassB $b)
    {}
}

class ClassB {}
class ClassWrong{}

正しい使用法:

$a = new ClassA;
$a->method_a(new ClassB);

エラーの生成:

$a = new ClassA;
$a->method_a(new ClassWrong);

キャッチ可能な致命的なエラー:ClassA :: method_a()に渡される引数1は、ClassBのインスタンスである必要があり、ClassWrongのインスタンスが指定されています...

そのエラーをキャッチすることは可能ですか(「キャッチ可能」と表示されているため)?はいの場合、どのように?

4

1 に答える 1

114

更新:これは、php 7ではキャッチ可能な致命的なエラーではなくなりました。代わりに、「例外」がスローされます。例外ではなくエラーから派生した「例外」(皮肉の引用符)。それはまだThrowableであり、通常のtry-catchブロックで処理できます。https://wiki.php.net/rfc/throwable-interfaceを参照してください

例えば

<?php
class ClassA {
  public function method_a (ClassB $b) { echo 'method_a: ', get_class($b), PHP_EOL; }
}
class ClassWrong{}
class ClassB{}
class ClassC extends ClassB {}


foreach( array('ClassA', 'ClassWrong', 'ClassB', 'ClassC') as $cn ) {
    try{
      $a = new ClassA;
      $a->method_a(new $cn);
    }
    catch(Error $err) {
      echo "catched: ", $err->getMessage(), PHP_EOL;
    }
}
echo 'done.';

プリント

catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassA given, called in [...]
catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given, called in [...]
method_a: ClassB
method_a: ClassC
done.

php7より前のバージョンの古い答え:
http ://docs.php.net/errorfunc.constantsによると:

E_RECOVERABLE_ERROR(整数)キャッチ
可能な致命的なエラー。おそらく危険なエラーが発生したが、エンジンを不安定な状態にしたわけではないことを示しています。エラーがユーザー定義のハンドルによってキャッチされない場合(set_error_handler()も参照)、アプリケーションはE_ERRORであるため中止されます。

参照:http ://derickrethans.nl/erecoverableerror.html

例えば

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    echo "'catched' catchable fatal error\n";
    return true;
  }
  return false;
}
set_error_handler('myErrorHandler');

class ClassA {
  public function method_a (ClassB $b) {}
}

class ClassWrong{}

$a = new ClassA;
$a->method_a(new ClassWrong);
echo 'done.';

プリント

'catched' catchable fatal error
done.

編集:しかし、try-catchブロックで処理できる例外にすることはできます

function myErrorHandler($errno, $errstr, $errfile, $errline) {
  if ( E_RECOVERABLE_ERROR===$errno ) {
    echo "'catched' catchable fatal error\n";
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    // return true;
  }
  return false;
}
set_error_handler('myErrorHandler');

class ClassA {
  public function method_a (ClassB $b) {}
}

class ClassWrong{}

try{
  $a = new ClassA;
  $a->method_a(new ClassWrong);
}
catch(Exception $ex) {
  echo "catched\n";
}
echo 'done.';

参照:http ://docs.php.net/ErrorException

于 2010-03-18T09:09:38.317 に答える