1

メソッドにアクセスする行を取得するにはどうすればよいですか?

方法:

function configure($newFile = false){

    try {

        ...
        throw new Exception("Error X");

    } catch (Exception $e) {

        $exception = "<b>Caught exception: </b>\n<blockquote>"
            .$e->getMessage()
            ."</blockquote>"
            ."\n"."on line <b>"
            .$e->getLine()
            ."</b> of <i>"
            .$e->getFile()
            ."</i>";

        echo $exception;

    }

}

出力は次のようなものです。

Caught exception:
Error X
on line 25 of C:\xampp\htdocs\MgFramework\classes\MgDatabase.class.php

しかし、そのメソッドにアクセスする行とファイルを表示したい:

$database = new MgDatabase();
$database->configure();

出来ますか?

ありがとう!

4

2 に答える 2

0

getTrace() メソッドにアクセスするだけで済みました。

スローまでのすべてのトレースを含む 2 次元配列を返します。

于 2012-12-10T09:27:37.783 に答える
0
class YourClass extends Exception
    {
        /**
         * can use
         * $this->line
         *
         * only __construct and __toString are not final
         *
         * @link http://php.net/manual/en/class.exception.php
         *  */
        function SomeMethod()
        {
            return "that is mine one: " . $this->line; // Exception extends variable access to protected variable $line
        }
        function AnotherMethod(Exception $e)
        {
            return $e->getLine() . " – " . $e->getFile();
        }
        function ThirdMethod($l,$f)
        {
            return $l . " – " . $f;
        }
    }

    $test = new YourClass;
    print $test->getLine(); // Exception extends method

    print "<hr>";
    print $test->SomeMethod(); // your class Method

    print "<hr>";
    print $test->AnotherMethod(new Exception()); // new Exception

    print "<hr>";
    print $test->ThirdMethod(__LINE__,__FILE__); // magic constant __LINE__ and __FILE__
于 2014-07-13T01:43:12.553 に答える