AMFPHP in convinient and reliable way

Remoting with AMFPHP greatly simplifies the client-server application logic. Now I’m going to simplify it’s usage. No responders, no listeners or clients. Just one call(...) and one callback function:

  1. private function init():void
  2. {
  3.     new Gateway("http://localhost/amfphp/gateway.php");
  4. }
  5.  
  6. private function listButton_clickHandler(event:MouseEvent):void
  7. {
  8.     listButton.enabled = false;
  9.     call("Persons.listPersons", 10, listPersonsCallback);
  10. }
  11.  
  12. private function listPersonsCallback(object:Object, errorText:String):void
  13. {
  14.     listButton.enabled = true;
  15.     listButton.errorString = errorText;
  16.     if (errorText)
  17.         return;
  18.    
  19.     // handle persons from object
  20. }

Here is the Persons.php service:

  1. < ?php
  2. class Persons
  3. {
  4.  
  5.     var $link;
  6.  
  7.     public function __construct()
  8.     {
  9.         $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
  10.         if (!$link)
  11.             throw new Exception("DB connection error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
  12.         if (!mysql_select_db(DB_NAME))
  13.             throw new Exception("DB selection error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
  14.     }
  15.  
  16.     function listPersons($limit)
  17.     {
  18.         $query = sprintf("SELECT * FROM persons LIMIT %d",
  19.             mysql_real_escape_string($limit));
  20.         $this->_handleResultError($result = mysql_query($query)));
  21.  
  22.         return $result;
  23.     }
  24.    
  25.     // utility function
  26.     function _handleResultError(&$result)
  27.     {
  28.         if (!$result)
  29.             throw new Exception("query error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
  30.     }
  31.  
  32. }

Download Gateway.as and call.as

Callback function can have the following signatures depending on the application logic:

  • function():void
  • function(errorText:String):void
  • function(object:Object, errorText:String):void

2 responses to “AMFPHP in convinient and reliable way”

  1. Nice! I belive, you forgot to mention where we should put the call.as file in order call()-function to be globally accessed =)
    I guess it should be placed in default package, shouldn’t it ?
    And I think it’s useful for small pure AS3 projects, because in Flex we have RemoteObject, and global call function is not OOP way =)

Leave a Reply