AMFPHP class mapping

AMFPHP, Flash, PHP Add comments

Here are some good things to know when using class mapping with AMFPHP. As an example, imagine working on an application that handles users through a UserVO value object. Here's what the PHP and the AS2 class look like.

The UserVO class in PHP

This class is in a "vo" subfolder of the "services" folder (in your AMFPHP installation dir).

PHP:
  1. class UserVO{
  2.   var $firstname;
  3.   var $lastname;
  4.   var $age;
  5.  
  6.   function UserVO(){
  7.   }
  8. }

The UserVO class in AS2

This class is in the namespace "com.domain.vo.UserVO".

Actionscript:
  1. class com.domain.vo.UserVO{
  2.   public var firstname:String;
  3.   public var lastname:String;
  4.   public var age:Number;
  5.  
  6.   function UserVO(){
  7.   }
  8. }

Flash to PHP

Sending VO's from Flash to PHP is quite simple. Just add the parameters in the "arguments" key. Use the fully qualified classpath of the class on the server side and make sure you set "required" to true;

PHP:
  1. "saveUser" => array(
  2.   "description" => "Inserts or updates the user in de database.",
  3.   "access" => "remote",
  4.   "arguments" => array(
  5.     "aboutVO" => array("type" => "vo.UserVO", "required" => true)
  6.   )      
  7. )

PHP to Flash

Watch out here! You can leave out the "returns" key from the method entry in the methodtable. When you do that, AMFPHP will return the name of the class for you. The thing to know here is that in PHP4 the classname will be returned in lowercase, while in PHP5 it will be returned in its original format of lower- and uppercases. Note that this does not return the fully qualified classpath, but just the classname. "uservo" vs. "UserVO".

Here's a quote from the "get_class" function docs.

In PHP 4 get_class() returns a user defined class name in lowercase, but in PHP 5 it will return the class name in it's original notation.
http://be.php.net/manual/en/function.get-class.php

This is not recommended because Object.registerClass() will fail because it is case-sensitive. The best way to get Object.registerClass() to work is to pass in the name of the class in the "returns" key and then use that same name in lowercase to register the class in Flash.

PHP:
  1. "getUserVO" => array(
  2.   "description" => "Returns a user.",
  3.   "access" => "remote",
  4.   "returns" => "vo.UserVO",
  5.   "arguments" => array()
  6. )

And then in Flash...

Actionscript:
  1. Object.registerClass("vo.uservo", com.domain.vo.UserVO);

That's it, cheers!

[Update] I must have missed something here: the "returns" string is always passed to Flash in lowercase. So remember that when you use Object.registerClass().


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

2 Responses to “AMFPHP class mapping”

  1. Patrick Mineault Says:

    Yeah, actually the lowerstring on returns thing is an error, I’ll fix that right now. There’s a typo in my name in your links list ;)

  2. xypyy Says:

    Mapping VO’s from Flex to PHP using AMFPHP
    visit this links http://xypyy2.blogspot.com/2008/01/mapping-vos-from-flex-to-php-using.html

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login