Introduction to using SabreAMF with Flex

Wil wrote a pretty good introduction to using SabreAMF with flex. He's making use of the Zend autoloader to load the classes. If you're new to SabreAMF, this might be a good starting point.

He also mentions its best to set display_errors to 0, to suppress PHP errors. I personally prefer re-throw all PHP errors as exceptions using the following class:

  1. <?php
  2.  
  3.     /**
  4.      * This is a default exception wrapper for PHP errors.
  5.      * This allows you to deal with PHP errors as exceptions (using try..catch blocks etc..)
  6.      *
  7.      * @uses Exception
  8.      * @package Sabre
  9.      * @subpackage PHP
  10.      * @version $Id: Exception.php 733 2007-05-15 22:04:52Z evert $
  11.      * @copyright Copyright (C) 2007 Rooftop Solutions. All rights reserved.
  12.      * @author Evert Pot (http://www.rooftopsolutions.nl)
  13.      */
  14.     class Sabre_PHP_Exception extends Exception {
  15.  
  16.         /**
  17.          * __construct
  18.          *
  19.          * @param string $message
  20.          * @param string $code
  21.          * @param string $file
  22.          * @param int $line
  23.          * @return void
  24.          */
  25.         function __construct($message,$code=false,$file=false,$line=false) {
  26.  
  27.             parent::__construct($message,$code);
  28.             $this->file = $file;
  29.             $this->line = $line;
  30.  
  31.         }
  32.  
  33.         /**
  34.          * Register this class as an error handler
  35.          *
  36.          * @return void
  37.          */
  38.         static function register() {
  39.        
  40.             set_error_handler(array('Sabre_PHP_Exception','handleError'));
  41.            
  42.         }
  43.  
  44.         static function unregister() {
  45.  
  46.             restore_error_handler();
  47.  
  48.         }
  49.  
  50.         /**
  51.          * handleError
  52.          *
  53.          * @param string $code
  54.          * @param string $message
  55.          * @param string $file
  56.          * @param int $line
  57.          * @return void
  58.          */
  59.         static function handleError($code,$message,$file,$line) {
  60.            
  61.             if (!$code) return;
  62.             throw new self($message,$code,$file,$line);
  63.  
  64.         }
  65.  
  66.     }
  67.  
  68. ?>

Simply call Sabre_PHP_Exception::register();, and you're off..


5 Responses to Introduction to using SabreAMF with Flex

  1. 509 blaz 2008-09-25 1:03 pm

    Hi,

    first great job for Sabre

    then I woold like to know if we can catch php Exception in the FaultResult handler on flex?

    thanks

  2. 510 blaz 2008-09-25 1:26 pm

    hum.. i receive the Exception on the e.fault.message

  3. 511 Evert 2008-09-26 3:32 am

    So you fixed it?

  4. 512 blaz 2008-09-26 6:29 am

    yes thanks,

    The probleme was coming from my code because i catch Exception somewhere and i return the exceptions :S

    I have seen this part of code in CallbackServer.php that help me :

    $response = new SabreAMF_AMF3_ErrorMessage($request['data']);
    $response->faultString = $e->getMessage();
    $response->faultCode = $e->getCode();
    $response->faultDetail = $detail;

    thanks

  5. 513 Evert 2008-09-26 12:25 pm

    Cool! Glad it works..

Leave a Reply



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.