Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://groups.adobe.com/groups/ab29539ab9.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

ActionScript, Debug, How to

Dump Debug Method (Like var_dump Function in PHP) and Debug Class

Andrei Ionescu | 25.06.08 | 8 Comments

Google Buzz

Although Flex Builder and Eclipse do have nice debug utilities built in, today I needed a function/method in Actionscript to display into the current application some variables’ content. So I started to implement a var_dump alike function (var_dump from PHP language).

So I started and I made the Debug class that will implement the static dump function. Looking at the PHP var_dump function I noticed two, three main problems that will appear:

  1. data types – ex: array is displayed as object by typeof function/operator
  2. length of objects and associative arrays – ex: length property for objects and associative arrays is undefined
  3. behaviour of static methods and variables inside calsses – ex: a static variable will keep its content even new methods calls appear, so calling the static dump method will not reinitialize the static vars.

For al these problems solutions are available:

  1. instead of using typeof we will use flash.utils.getQualifiedClassName static method (see also this old article) – more about typeof operator and its return values here
  2. we will implement a method to count the first children in an object
  3. there will be an internal recursiveDump function and a generic public dump function

Here is the application code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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>

The Debug class with its static methods can be downloaded at the end but now you can see the dump of the above flex code.

Share and Enjoy:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites
var_dump_app
var_dump_sources




Tags: , , ,

This post was written by Andrei Ionescu

Views: 5830

related

8 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


«
»