Skip to Content »

FlashApe » archive for August, 2008

 Flex + AMFPHP: Mapping Value Objects

  • August 2nd, 2008
  • 5:09 pm

Update: Added code samples to end of post.

One of the issues I see a lot of with people using AMFPHP is how to properly map VO classes between Flex and php. It used to stress me out too, until I realized how easy it really is.

First thing: Don't worry about the package paths in the RemoteClass metadata tag and $_explicitType variables in your php classes. You don't need them.

Second thing: Just use the name of the class you are mapping in for the values RemoteClass("alias="") and $_explicitType="". For example, if you wanted to map a value object named 'Book' between flex and php, just use:

Actionscript:
  1. [RemoteClass(alias="Book")]

and

PHP:
  1. var $_explicitType="Book";

Like i already mentioned, you don't need the package names. The reasons behind this are:

  • When sending from php to Flex, Flex will match the $_explicitType variable to the Remote class alias. It's just an id to match...it doesn't even have to be the class name...it can be jibberrish, as long as both strings match. (This won't work the other way though...see next item)
  • When sending from Flex to php, AMFPHP will try to load the class with the name specified by the RemoteObject alias value. It simply looks in a specified directory to try and load the class...if it can't find it it's parsed as an array.

So, how does AMFPHP know where to look for the vo classes? Easy...you tell it exactly which directory to look in by setting the $voPath variable in globals.php. I usually do something along the lines of :

PHP:
  1. $voPath = $_SERVER["DOCUMENT_ROOT"]."/path/to/vo/directory";

Here's a more flushed out code sample:

Actionscript:
  1. package com.website.vos
  2. {
  3.     [RemoteClass(alias="Book")]
  4.     [Bindable]
  5.     public class Book
  6.     {
  7.        
  8.         public var title:String;
  9.         public var author:String;
  10.        
  11.         public function Book()
  12.         {
  13.         }
  14.        
  15.  
  16.     }
  17. }

PHP:
  1. <?php
  2.  
  3. class Book {
  4.  
  5.     var $_explicitType="Book";
  6.     var $title;
  7.     var $author;
  8.  
  9.  
  10. }

Say for example the Book.php class file is sitting on the server at webroot/php/classes/vos/Book.php. In order to tell AMFPHP where to look for the Book.php file, you open up globals.php in your AMFPHP installation, and set the $voPath variable to point to the 'vos' folder:

PHP:
  1. $voPath = $_SERVER["DOCUMENT_ROOT"]."/php/classes/vos";

Now when you send a Book object from Flex to php, AMFPHP will look in the $voPath (which we just set to the 'vos' folder) for the Book.php file.

Note that the $voPath variable is the starting point of where AMFPHP will start to look. If you specify a full package path for the RemoteClass alias, such as:

Actionscript:
  1. [RemoteClass(alias="com.website.vos.Book")]

then all the dots in the string are converted to slashes, and the full path is used. To continue with the example above, we have already set $voPath to point to the 'vos' folder, and the RemoteClass alias has been set to "com.website.vos.Book". AMFPHP converts the alias to "com/website/vos/Book" and will attempt to locate the file $voPath/com/website/vos/Book.php. In addition, if you're mapping this object from php back top Flex, remember the $_explicitType variable still must match the string for the RemoteClass alias, so you'd need to set that to:

PHP:
  1. var $_explicitType="com.website.vos.Book";