Blog to discuss Midnight Coders products, features, ideas and trends in development of Rich Internet Applications

Monday, September 08, 2008

Debugging PHP code when invoked from Flex

Among thousand other things we have been doing quite a bit of work on WebORB for PHP lately. One of the most unpleasant things in PHP is the complete lack of normal debugging tools (at least I could not find any decent debuggers that would work for me). As a result, when PHP is invoked from a rich client like Flex, one must get very creative in order to tap into the PHP invocation flow and debug it. The reason traditional echo() does not work is it would mess up the binary output created by the gateway. There are several techniques I found to be very effecive. Below is the full list:

  1. Use native PHP logging. PHP has fairly rich system for error reporting which could be used to log any kind of messages (not just errors). It is important to make sure that your PHP process (or the process running PHP, like IIS/ISAPI for instance) have enough permissions to write to files. Then modify your PHP.INI and enable the following properties: (you can find out where your PHP.INI is by running a PHP page that executes phpinfo())
    log_errors = On
    error_log = C:\some-directory-path\errors.txt
    Once you changed PHP.INI, restart your PHP process and you should be able to log your own errors. There's a function that lets you do that: error_log. For instance, the following line will save a string in the error log file:

    error_log( "yo, log this message",  0 );
    To get a detailed view of a complex type or an array, use print_r, but make sure to pass the second parameter to capture the value. For instance:

    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    error_log( "contents of the array are " . print_r($a, true), 0 );

  2. Use WebORB Management Console. The console has a feature called Test Drive. Once you deploy your code into the /services folder, you will see your class in the Management > Services tab. Locate the class, expand it and select the method you want to invoke. You will be able to enter argument values right on the screen and invoke the method using the "Invoke" button.

  3. Use Charles or ServiceCapture proxies. Either one of these tools is a MUST for anyone developing an RIA. You can see all the traffic between your rich client running in a browser and the server. Moreover, the tools decode the binary traffic and display invocation and response structures in a tree view format. If you are getting Channel Disconnected error, make sure to inspect the server (PHP) response with one of these tools. (I should probably repeat that 10 times, so it is implanted into the brains of Flex/PHP developers).

  4. Use WebORB logging. WebORB logging provides more granular control over what gets logged. You can also enable/disable individual loggers in the management console (Management > Server Configuration > Logging). WebORB logging dumps the log messages into a file under /logs folder (located in the weborb installation directory). Using the logging API is fairly straightforward. First "import" the classes using:

    require_once(WebOrb . 'Util/Logging/Log.php');
    require_once(WebOrb . "Util/Logging/LoggingConstants.php");

    Then you can start logging using the following call:

    Log::log(LoggingConstants::INFO, "your message or object here" );

    There are several logging categories available, see Util/Logging/LoggingConstants.php for details. Each category can be turn on/off in the management console.

If you have any tips and tricks for PHP/Flex debugging, please post your comments.


Blogger Eric said...

I develop on Mac OSX Leopard with Flex Builder 3. I can use Flex Builder 3 to debug Fx or Air applications that use WebOrb PHP simultaneously with XDebug running in PDT. Sweet!

Briefly, to debug the AIR application I'm working on currently...

-- I used MacPorts to install Apache 2, PHP 5, MySQL 5, and XDebug.
-- I configured XDebug in PHP.ini as follows:


-- I DID NOT integrate an Eclipse PHP environment with Flex Builder 3. Instead, I downloaded the "all-in-one" PDT 1.0 Eclipse IDE from This includes the XDebug Client and everything else you need, and worked out-of-the-box.

-- My project properties in Eclipse PDT include the following:
Resource: /Services
Type: Project
Location: /path-to-WebOrb-Services_dir

I created a symbolic link to the WebOrb Services directory in the root directory of my local PHP Development Web Server to facilitate the Debug configuration discussed next.

In Open Debug Dialog, I created a PHP Web Page Debug configuration with

---Server Tab---
Server Debugger: XDebug
PHP Server: my local PHP Development Web Server
File: a PHP Class file in services that I wanted to debug
URL: checked Auto Generate

When you start a Debug Session with this configuration, it launches the browser. You can close it, it isn't needed.
You can place break points in the PHP Class file that you specified on the Debug Dialog Server Tab. At this point, the PDT XDebug client is waiting for a call to the class file that sets the XDEBUG_SESSION_START session variable, in your RemoteObject like so:


Some References that could prove helpful:

12:37 PM

Blogger dylan said...

I can attest - XDebug works great for debugging PHP services for Flex. In contrast to Eric, I just set breakpoints, start the PHP debug session, *leave that window open*, and open my Flex app. The PHP debugger wakes up and breaks as it's triggered by calls from Flex.

11:00 AM


Post a Comment

<< Home