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:
- 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 = OnOnce 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 = C:\some-directory-path\errors.txt
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 );
- 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.
- 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).
- 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.