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:
-
private function init():void
-
{
-
new Gateway("http://localhost/amfphp/gateway.php");
-
}
-
-
private function listButton_clickHandler(event:MouseEvent):void
-
{
-
listButton.enabled = false;
-
call("Persons.listPersons", 10, listPersonsCallback);
-
}
-
-
private function listPersonsCallback(object:Object, errorText:String):void
-
{
-
listButton.enabled = true;
-
listButton.errorString = errorText;
-
if (errorText)
-
return;
-
-
// handle persons from object
-
}
Here is the Persons.php
service:
-
< ?php
-
class Persons
-
{
-
-
var $link;
-
-
public function __construct()
-
{
-
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
-
if (!$link)
-
throw new Exception("DB connection error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
-
if (!mysql_select_db(DB_NAME))
-
throw new Exception("DB selection error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
-
}
-
-
function listPersons($limit)
-
{
-
$query = sprintf("SELECT * FROM persons LIMIT %d",
-
mysql_real_escape_string($limit));
-
$this->_handleResultError($result = mysql_query($query)));
-
-
return $result;
-
}
-
-
// utility function
-
function _handleResultError(&$result)
-
{
-
if (!$result)
-
throw new Exception("query error" . (PRODUCTION_SERVER|> ? "" : ": " . mysql_error()));
-
}
-
-
}
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
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 =)
Yes, you are right about
call()
importing. Don’t forget to importMouseEvent
too! :)I don’t think that one global method can harm your OOP structure.
navigateToURL()
is global too. Important thing is just not to create a lot of unnecessary global methods and properties.