その他

FlexでObjectのvar_dump()を行う「Debug.dump」を試してみた

こんにちは。松田です。
最近ずっとFlexばかりいじってるわけですが、PHPではおなじみのvar_dumpをFlexで実装した、「Debug.dump」があったので紹介します。
Objectインスタンスは中身を確認しづらいので、PHPに慣れた人にとってはとても使いやすそうです。

ダウンロードは以下のURLから
http://www.flexer.info/2008/06/25/dump-debug-method-like-var_dump-function-in-php-and-debug-class/

ページ中段の 「var_dump_sources」 をクリックするとソースのzipファイルがダウンロードできます。
それを解凍するとcomディレクトリが出てくるので、それを読み込み可能な位置に配置します。
そしてscript内からcom.flexer.Debugをimportすれば準備完了ですっ。

サンプルのコードは以下のとおりです。


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
layout="absolute" width="500" height="400"
creationComplete="start()">
<mx:TextArea id="debugTA" x="10" y="10" 
width="480" height="380"/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import com.flexer.Debug;
private var testdata:Object;
private function start():void
{
// even though the object is created like here
// the order does not reflect the order of
// creation - the order is somehow made internally 
testdata = new Object();
testdata["row1"] = {
"col1":"a",
"col2":{
"12":"12",
"13":[1,2,3,4]
},
"col3":"c",
"col4":"d"
};
testdata["row2"] = new XML();
testdata["row2"] = <test><tag1 att="att">val</tag1></test>;
testdata["row3"] = {"col1":"a","col2":"b","col3":"c","col4":"d"};
testdata["row4"] = {"col1":"a","col2":"b","col3":"c","col4":"d"};
testdata["row5"] = new Array();
testdata["row5"] = ["a","b","c","d"];
testdata["row6"] = {"col1":"a","col2":"b","col3":"c","col4":"d"};
// calling debug.dump
debugTA.text = Debug.dump(testdata,3,5.5);
}
]]>
</mx:Script>
</mx:Application>

これを実行した結果が下のようになります。


Object (6) {
    [row2] => 
    XML(<test>
  <tag1 att="att">val</tag1>
</test>)
    [row3] => 
    Object (4) {
        [col3] => 
        String (1) = "c"
        [col4] => 
        String (1) = "d"
        [col1] => 
        String (1) = "a"
        [col2] => 
        String (1) = "b"
    }
    [row5] => 
    Array (4) {
        [0] => 
        String (1) = "a"
        [1] => 
        String (1) = "b"
        [2] => 
        String (1) = "c"
        [3] => 
        String (1) = "d"
    }
    [row1] => 
    Object (4) {
        [col3] => 
        String (1) = "c"
        [col4] => 
        String (1) = "d"
        [col1] => 
        String (1) = "a"
        [col2] => 
        Object (2) {
            [12] => 
            String (2) = "12"
            [13] => 
            Array (4) {
                [0] => 
                int(1)
                [1] => 
                int(2)
                [2] => 
                int(3)
                [3] => 
                int(4)
            }
        }
    }
    [row4] => 
    Object (4) {
        [col3] => 
        String (1) = "c"
        [col4] => 
        String (1) = "d"
        [col1] => 
        String (1) = "a"
        [col2] => 
        String (1) = "b"
    }
    [row6] => 
    Object (4) {
        [col3] => 
        String (1) = "c"
        [col4] => 
        String (1) = "d"
        [col1] => 
        String (1) = "a"
        [col2] => 
        String (1) = "b"
    }
}
int(3)
Number(5.5)

PHPに慣れた人にとってはとっても見やすいフォーマットですね。
このdumpメソッドですが、サンプルコードにもあるようにObjectだけでなくXMLのインスタンスもvar_dumpしてくれるようです。
通常Objectの中身を見るにはfor each等を駆使するしかなく、多重階層の場合は非常に苦労していたので、これはデバッグには欠かせないツールになりそうです。

author img for asial

asial

前の記事へ

次の記事へ

一覧へ戻る
PAGE TOP