21
22

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.

例外の継承関係を可視化するショートコード

21
Last updated at Posted at 2014-03-26
ソースコード
<?php
$lines = array();
foreach (preg_grep('/Exception\z/i', get_declared_classes()) as $class) {
    $classes = array();
    do {
        $classes[] = $class;
    } while ($class = get_parent_class($class));
    $lines[] = implode(' -> ', array_reverse($classes));
}
sort($lines);
echo implode(PHP_EOL, $lines);
実行結果の例
Exception
Exception -> DOMException
Exception -> ErrorException
Exception -> LogicException
Exception -> LogicException -> BadFunctionCallException
Exception -> LogicException -> BadFunctionCallException -> BadMethodCallException
Exception -> LogicException -> DomainException
Exception -> LogicException -> InvalidArgumentException
Exception -> LogicException -> LengthException
Exception -> LogicException -> OutOfRangeException
Exception -> PharException
Exception -> ReflectionException
Exception -> RuntimeException
Exception -> RuntimeException -> OutOfBoundsException
Exception -> RuntimeException -> OverflowException
Exception -> RuntimeException -> PDOException
Exception -> RuntimeException -> RangeException
Exception -> RuntimeException -> UnderflowException
Exception -> RuntimeException -> UnexpectedValueException
Exception -> RuntimeException -> mysqli_sql_exception

PHP言語自体の開発者に言いたい文句

  • なんで IOException って存在しないの
  • IOException が無いなら Exception とすべきところをなんで RuntimeException を投げる
  • 何で mysqli_sql_exception だけスネークケースなの
  • 何で固有例外クラスのうち PDOExceptionmysqli_sql_exception だけが RuntimeException を継承してるの
個人的にこうあってほしい
Exception
Exception -> DatabaseException
Exception -> DatabaseException -> MysqliExeption
Exception -> DatabaseException -> PDOException
Exception -> DOMException
Exception -> ErrorException
Exception -> IOException
Exception -> LogicException
Exception -> LogicException -> BadFunctionCallException
Exception -> LogicException -> BadFunctionCallException -> BadMethodCallException
Exception -> LogicException -> DomainException
Exception -> LogicException -> InvalidArgumentException
Exception -> LogicException -> LengthException
Exception -> LogicException -> OutOfRangeException
Exception -> PharException
Exception -> ReflectionException
Exception -> RuntimeException
Exception -> RuntimeException -> OutOfBoundsException
Exception -> RuntimeException -> OverflowException
Exception -> RuntimeException -> RangeException
Exception -> RuntimeException -> UnderflowException
Exception -> RuntimeException -> UnexpectedValueException
21
22
10

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
21
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?