8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

__toString()でExceptionが発生すると死ぬ

Posted at
<?php
class Hoge {
  function __toString() {
    throw new Exception;
  }
}

try {
  $hoge = new Hoge;
  echo $hoge;
} catch (Exception $e) {
}

try ~ catchで拾っているので動作しそうなコードに見えるけれど、実行すると
PHP Fatal error: Method Hoge::__toString() must not throw an exception
となって死ぬ。

Exceptionぐらい発生してもええやん!!と思うのだが実装が難しいのだとかナントカ。
https://bugs.php.net/bug.php?id=53648

とりあえず今の段階では__toString()の内部にtry {} catch()を書いて自衛するしかない。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?