Class mapping

From version 1.2 onwards, amfphp has complete class mapping options. That means that if you want to send typed objects through the wire, or VOs, it's simple to achieve both through and from amfphp.

Mapping from Flash to amfphp

The first thing that you'll need to do is call Object.registerClass to register the class to Flash. For example, if you have a class called 'com.company. MyClass', call:

Object.registerClass('com.company. MyClass', com.company. MyClass)

From that point onwards whenever you send an instance of the class across the wire, the class will carry along its class name. If you want to map this class naturally to a PHP class, first, set the location of mapped classes in advancedsettings.php; by default, this is set to services/vo/. Place a class by the same name as the Flash file in a corresponding php file. For example, if the Flash class is called 'com.company.MyClass', create a class called MyClass in services/vo/com/company/MyClass.php. From that point on, instead of receiving an associative array in PHP, you will receive a class instance. Here's how it works:

  • Object.registerClass puts class name in sent AMF
  • amfphp picks up class name, looks for a file called 'classname.php' or classname.class.php' in the customMappingPath folder.
  • If the file is found, it is included
  • It the include was succesful, amfphp checks if the class by he same name exists
  • If it does, then amfphp instantiates it and fills its properties with the object properties
  • If the class has an init function, amfphp calls it
  • You receive a class instance transparently in your service

If you want to map a Flash class with one name with a php class with another name, you can use the $incoming array in advancedsettings.php for that purpose.

Mapping from PHP to Flash

There are two issues that prevent mapping from PHP to Flash to be completely tranparent:

  1. PHP has no packages
  2. PHP4 returns class names as lowercase

To get around this issue, the simplest solution is to set a special key in the class you want to return to Flash called '_explicitType'. The explicitType may contain the package name and can be uppercase or lowercase.

Another way is to systematically map all instances of one PHP class to a corresponding Flash class using the global $outgoing array in advancedsettings.php. Read the comments there for more info.


© amfphp.org | Disclaimer | Conditions of use