Passing on optional arguments in ActionScript 3

The other day I ran into a small problem with optional arguments in Flex. While writing my tutorial on AMFPHP and Flex I decided to create a small static class that would take care of all the boring stuff with calling AMFPHP. This class enables me to simply use AMFPHP.send(“ServiceName”, and so on).

However, writing the class I realized I wouldn’t be able to pass on the optional parameters. If I would use:

function send(anArgument:Object, ...args)

…I wouldn’t be able to send the args argument to the NetConnection.call method, because actionscript would send a “bunched up” array with all the arguments as ONE parameter to the call method, as opposed to a series of parameters, which was what I wanted.

The solution to this is using the Function.apply() method. What I did was to create an empty array and add a series of arguments I wanted to pass on to a function, create a reference to the function and then use the apply method. This is what my finished static method looks like:

public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, ... args:*) : void
{
	trace("AMFPHP("+serviceFunction+")");
	// Create responder
	var responder:Responder = new Responder(resultHandler, faultHandler);
	// Create an array that will temporarily store all the arguments
	var collectArgs:Array = new Array;
	// Add the fixed arguments
	collectArgs.push(serviceFunction);
	collectArgs.push(responder);
	// Loop through the optional arguments and add them too
	for (var i:uint=0; i

AMFPHP and Flex

Being a LAMP (Linux Apache MySQL PHP) guy as well as a Flex developer it didn’t take me long to discover AMFPHP. If you haven’t heard of it I can tell you in short it’s a framework for sending and receiving data between Actionscript and PHP, without having to use an XML layer or similar in between, so to speak. It’s really neat, because lets say you extract an array of rows from your MySQL database, then you can just pass the entire array back to Actionscript and it will be received as an Actionscript array in the other end.

Would you like to try it? This is how to do it:

1. Download the latest AMFPHP package from www.amfphp.org

2. Unpack and (preferably) place the folder called amfphp in the document root on your web server.

3. By default the AMFPHP gateway is set to converting all UTF-8 data to Latin 1. Big NO NO for “international” developers like me, so you should open the gateway.php file in the amfphp folder for editing and change the following line (it should be around line 127):

$gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");

to

$gateway->setCharsetHandler("iconv","UTF-8","UTF-8");

If you don’t do this all UTF-8 special characters from the database will be distorted on arriving at the Flex application.

4. Now create a simple PHP class file, call it HelloWorld.php, and place it in the /amfphp/services folder. You should use a commenting pattern like that of PHPDoc. Here is a simple HelloWorld example:

/**
 * A simple amfphp service.
 */
class HelloWorld
{
	/**
	 * A simple HelloWorld function
	 * @returns A string containing the phrase 'Hello World!'
	 */
	function SayHello()
	{
		return "Hello World!";
	}

	/**
	 * A simple HelloWorld function that bounces back the given string
	 * @returns A string containing the supplied string
	 */
	function SayWhat($string)
	{
		return $string;
	}
}

In this class I’ve put two functions; one that returns the string ‘Hello World!’ and another that returns whatever string you send to it. Okay, so let’s get on with the show…

5. Now you can test your service by browsing to the browser folder. It should have a URL similar to this: yourserver.com/amfphp/browser. In the service browser you can test whether your class is working as it should.

6. Flex
To simplify things I’ve created a small class for the implementation of this in Flex. There is a number of ways of doing this, but I’ve chosen the one way that I am most comfortable with.

For ease of use, create an actionscript document in your Flex Projects root directory called AMFPHP.as and past the following code into the file:

package {
	import flash.net.NetConnection;
	import flash.net.Responder;

	public class AMFPHP
	{
		private static var gateway:String = "http://www.yourserver.com/amfphp/gateway.php";
		private static var connection:NetConnection = new NetConnection;
		connection.connect(gateway);

		public function AMFPHP() : void
		{
			// Static class
		}

		public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, ... args:*) : void
		{
			trace("AMFPHP("+serviceFunction+")");
			// Create responder
			var responder:Responder = new Responder(resultHandler, faultHandler);
			// Create an array that will temporarily store all the arguments
			var collectArgs:Array = new Array;
			// Add the fixed arguments
			collectArgs.push(serviceFunction);
			collectArgs.push(responder);
			// Loop through the optional arguments and add them too
			for (var i:uint=0; i

The only thing you have to change here is the URL to the gateway.php file on your server at line 7 in the file.

7. Usage

To use this in your code you will need three things:

• Calling the service using the static AMFPHP.send() function
• A function that handles the result coming from amfphp
• A function that handles errors

This is the first HelloWorld example (without the argument). The first argument for the send() function is a string describing the path to the function, with this structure: [directory.]class.function, within the services folder. A directory is not necessary, but can be convenient if you have packages of many classes.

private function helloWorld() : void
{
	AMFPHP.send("HelloWorld.SayHello",onResult,onFault);
}

private function onResult(result:String) : void
{
	trace(result);
}

private function onFault(result:Object) : void
{
	trace(String(result.description));
}

Easy huh!?

And the example with the argument is pretty much the same, but with an argument added to the end of the function call. Like this:

private function helloWorld() : void
{
	AMFPHP.send("HelloWorld.SayWhat", onResult, onFault, "Hello to you!");
}

private function onResult(result:String) : void
{
	trace(result);
}

private function onFault(result:Object) : void
{
	trace(String(result.description));
}

Note that I changed the path to the function from HelloWorld.SayHello to HelloWorld.SayWhat to use the other function in the class.

Well, that's it. Hope it was of some use. Good luck!