Automatic AMF class mapping in Zend (Part 1)

 Jun, 11 - 2009   2 comments   How To

First off, much thanks to Lee Coltrane for figuring this out. He’s a bit busy at the moment, so I’m writing this up without the benefit of his input which I’m sure will be extremely valuable once our schedules mesh up.

A few caveats right off the bat. 1) We’re still figuring out our best practices. 2) This is Flex only (see below)  3) We haven’t (yet) written any code to automatically populate the $_explicitType variable going back to Flash from PHP.

If you’re reading this, you’ve more than likely come across a Flex metatag like the one below:

[RemoteClass(alias="com.domain.FooBar")]

Personally, I found it annoying to have to populate the alias manually. The compiler knows the package name and the class name. Don’t make me type it again! And, in fact, the compiler will accept the metatag without it:

[RemoteClass]

The only problem is getting Zend (in our case) to map it to the correct remote class. The Adobe engineers saw fit to prepend a greater-than character to the outgoing classname when using this syntax. So, if you omit the alias, “>com.domain.FooBar” will be sent instead.

To get ZendAmf to map this automatically, you’ll need to make a slight modification to the Deserializer.php class. Mine is located in Zend/Amf/Parse/Amf3/Deserializer.php (Note: I haven’t even looked at the Amf0 class yet, but my understanding is that custom class mapping isn’t supported in AMF0) Insert the following code around line 315 in the readObject method after it has tried to determine the className but before it has assigned a return object.

// Allow and map automatic aliases via RemoteClass metatag & flex
if (strpos ($className, '>') === 0)
{
  $className = substr ($className, 1);
}
//translation: If the classname begins with '>' use the rest of the string instead.

This addition will deal with incoming class mapping. If you want to remove the alias parameter from all of your ActionScript RemoteClass metatags, you’ll need to format your $_explicitType entries like this:

$_explicitType = ">com.domain.FooBar";

…which is what we’ll look at in Part 2! 

NOTE: I said at the very beginning this is Flex only. Compiling with the Flash IDE does not support the RemoteClass metaTag, so you’ll need to use registerRemoteClass as you’ve probably read about elsewhere. Perhaps in part 2 (or 3?) I’ll write up an ActionScript class that can register classes against the correct package names automatically. Until then, I’ll give you a hint: flash.utils.describeType.


Related articles

 Comments 2 comments

  • Kramer auto Pingback[…] 2. Automatic AMF class mapping in Zend (Part 1)    karoshiethos.com […]


  • Leave a Reply

    Your email address will not be published. Fields with * are mandatory.