導入 (Previous) (Next) PHP_UML

View this page in Last updated: Sun, 31 Aug 2008
English | French | German | Japanese | Plain HTML

基本的な使用法

基本的な使用法 --  関数の引数や返り値などの単純なトレース

概要

traceArguments() を使用して、関数の引数をトレースします。 トレースをする際に、関数の引数を traceArguments() に渡す必要はありません。その関数が他の関数をコールしない場合、 あるいはその関数がコールする関数はトレースしないという場合については traceArguments() は使用しないこともあります。

traceVariables() を使用して、関数内の変数をトレースします。 トレースする変数を traceVariables() の引数として渡す必要があります。 traceVariables() は、 ウォッチ対象の変数の値が変わるたびにコールされることになります。

traceReturn() を使用して、関数の返り値をトレースします。 トレースする返り値を traceReturn() の引数として渡す必要があります。

putTrace() を使用して、 トレース結果を標準出力やファイルに書き出します。

PHP_FunctionCallTracer の使用法

この例では、 package.php の つのクラスのメソッドをトレースしてみます。 このパッケージは trace.php というアプリケーションで用いられており、 トレース結果は trace.txt に保存します。 トレースを作成するには #php trace.php を実行します。

例 58-1package.php パッケージ

PHP_FunctionCallTracer がロードされているかどうかを class_exists('PHP_FunctionCallTracer', false) で確認するのは必須ではありません。 これは、トレース用のメソッドをコードの中に残しておく場合にのみ有用です。 こうしておけば、PHP_FunctionCallTracer がロードされている場合にのみトレースメソッドを実行するようになります。


<?php
class math {
    /**
     * tracing the arguments and the returned parameter
     *
     * note that traceReturn() calls traceArguments() by default which is fine here
     * since this method does call other methods to trace
     */
    public static function prod($x$y)
    {
        // class_exists('PHP_FunctionCallTracer', false)
        // and PHP_FunctionCallTracer::traceArguments();
        $p $x $y;

        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceReturn($p);
        return $p;
    }

    /**
     * tracing the arguments and the returned parameter
     *
     * traceArguments() must be called here since this method calls other methods
     * that may be traced, so that traced calls are displayed in the right order
     */
    public static function square($x)
    {
        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceArguments();

        $x2 self::prod($x$x);

        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceReturn($x2);
        return $x2;
    }
}

class geometry {
    private $pi 3.14;

    /**
     * tracing the arguments and the returned parameter
     * another variable is traced along with the returned parameter
     */
    public function circle($r)
    {
        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceArguments();

        $pi2 $this->pi;
        $c math::prod($r$pi2);

        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceReturn($c$pi2);
        return $c;
    }

    /**
     * tracing the arguments, some variables and the returned parameter
     */
    public function disk($r)
    {
        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceArguments();

        $r2 math::square($r);
        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceVariables($r2$this->pi);

        $d math::prod($r2$this->pi);

        class_exists('PHP_FunctionCallTracer'false) and
        PHP_FunctionCallTracer::traceReturn($d);
        return $d;
    }
}
?>

例 58-3トレース結果 trace.txt

Array
(
    [php_uname] => Windows NT mybox 5.1 build 2600
    [date] => Friday, 03-Aug-07 09:17:30 UTC
    [calls] => Array
        (
            [0] => Array
                (
                    [call] => Array
                        (
                            [file] => trace.php
                            [line] => 20
                            [function] => geometry->circle
                        )
                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 55
                            [args] => Array
                                (
                                    [0] => 2
                                )
                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 61
                            [args] => Array
                                (
                                    [0] => 12.56
                                    [1] => 6.28
                                )
                        )

                )

            [1] => Array
                (
                    [call] => Array
                        (
                            [file] => package.php
                            [line] => 58
                            [function] => math::prod
                        )

                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 2
                                    [1] => 6.28
                                )
                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 12.56
                                )
                        )

                )

            [2] => Array
                (
                    [call] => Array
                        (
                            [file] => trace.php
                            [line] => 21
                            [function] => geometry->disk
                        )

                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 71
                            [args] => Array
                                (
                                    [0] => 3
                                )
                        )

                    [watches] => Array
                        (
                            [0] => Array
                                (
                                    [file] => package.php
                                    [line] => 75
                                    [args] => Array
                                        (
                                            [0] => 9
                                            [1] => 3.14
                                        )
                                )

                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 80
                            [args] => Array
                                (
                                    [0] => 28.26
                                )
                        )

                )

            [3] => Array
                (
                    [call] => Array
                        (
                            [file] => package.php
                            [line] => 73
                            [function] => math::square
                        )

                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 35
                            [args] => Array
                                (
                                    [0] => 3
                                )
                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 40
                            [args] => Array
                                (
                                    [0] => 9
                                )
                        )

                )

            [4] => Array
                (
                    [call] => Array
                        (
                            [file] => package.php
                            [line] => 37
                            [function] => math::prod
                        )

                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 3
                                    [1] => 3
                                )
                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 9
                                )

                        )

                )

            [5] => Array
                (
                    [call] => Array
                        (
                            [file] => package.php
                            [line] => 77
                            [function] => math::prod
                        )

                    [in] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 9
                                    [1] => 3.14
                                )
                        )

                    [out] => Array
                        (
                            [file] => package.php
                            [line] => 22
                            [args] => Array
                                (
                                    [0] => 28.26
                                )
                        )
                )
        )

    [objects] => Array
        (
            [0] => geometry Object
                (
                    [pi:private] => 3.14
                )
            [2] => same as #0
        )

)
導入 (Previous) (Next) PHP_UML

Download Documentation Last updated: Sun, 31 Aug 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
There are no user contributed notes for this page.