AMFPHP Class Mapping Primer [1.9 beta]

Thought I’d post a simple AMFPHP class mapping primer.

I ran into a wall after re-arranging my VO / DAO package structure on the server, learned a few things in the process and thought I’d post .

I’m assuming your using AMFPHP 1.9 beta.

Class mapping allows your php backend and actionscript frontend to pass strongly typed variables back and forth. The benefits are obvious so I won’t cover them here.

If your not after any detailed information here’s a quick set of samples classes to get you going.

PHP


// LoginVO.php is saved as {$voPath}/com/dl/app/LoginVO.php
class LoginVO
{
   var $_explicitType="com.dl.app.LoginVO";
   public $username;
   public $password;
}

AS

package com.dl.common.Login.model.vo
{
   [RemoteClass(alias="com.dl.app.LoginVO")]
   public class LoginVO
   {
      public var username:String;
      public var password:String;
   }
}

That’s it. If your after more options and detailed info, keep reading.

Sending Strongly Typed Objects from PHP to ActionScript via AMFPHP

In PHP

You have a few options (choose one).

  • Your class can declare a variable named $_explicitType;
  • The most common approach is to use the $_explicitType class member:

    class LoginVO
    {
       var $_explicitType="com.dl.app.LoginVO";
       public $username;
       public $password;
    }
    
  • You can set $GLOBALS[‘amfphp’][‘outgoingClassMappings’][‘localclass‘] = ‘remoteclassAlias‘;
  • e.g.

    $GLOBALS['amfphp']['outgoingClassMappings']['myclass'] = "com.dl.myapp.MyClass";

    Note that the local class name associative key (‘myclass’ above) must be in lowercase with the current release of AMFPHP.

  • Rely on the reflection class (PHP 5+)
  • If your install of PHP has the ReflectionAPI you can let AMFPHP do the work *for* you. I found a few cavets using this approach on servers who’s OS uses backslashes natively. If this is the case, AMFPHP will only send the name of the class and not include any package info. There are two simple code changes you can make in AMFPHP to get around this, just drop me a line if your interested (hopefully they’ll be commited in the next release of AMFPHP).

    In actionscript

    Here to, you have a some options,

  • Use [RemoteClass] metadata tag to specify the remote class alias
  • package com.dl.common.Login.model.vo
    {
       [RemoteClass(alias="com.dl.app.LoginVO")]
       public class LoginVO
       {
          public var username:String;
          public var password:String;
       }
    }
  • Use registerClassAlias()
  • registerClassAlias(“com.dl.app.LoginVO”,LoginVO);

    Make sure your actionscript project contains a reference to the class. See my previous post for more info.


    Sending Strongly Typed Objects from ActionScript to PHP via AMFPHP

    In PHP

    Rely on the class having been registed in ActionScript with the server’s correct package [directory] structure (relative to AMFPHP’s VO base path $GLOBALS[‘amfphp’][‘customMappingsPath’]). If this is the case you, your all set.

    Note: If you haven’t already you’ll need to make sure $voPath is pointing to the correct directory in AMFPHPs global.php

    Define $GLOBALS[‘amfphp’][‘incomingClassMappings’][‘remoteclassAlias‘] = ‘localclassPackage‘. (e.g. $GLOBALS[‘amfphp’][‘incomingClassMappings’][‘com.dl.as3.userVO’] = ‘com.dl.userVO’);

    In ActionScript

    Again, you’ll need to use one of the two following…

  • Use [RemoteClass] metadata tag to specify the remote class alias
  • Use registerClassAlias()

    For more info check out

    AMFBaseSerializer.php’s getClassName() function

    AMFBaseDeserializer.php’s mapClass() function