Typed Objects

With AMF it is possible to type objects. This is useful mostly to receive objects from the server that are already typed and therefore do not need parsing. This is a more advanced functionality and for a simple project it’s not worth the trouble and anonymous objects are fine. However for something more complex it can be worth the time to set this up. Most other protocols such as JSON don’t support object typing, but you can still set the “_explicitType” field in your data if you want to transmit the information back and forth from PHP.

To return a typed object from Amfphp, you need to do 2 things:

Server Side

Make sure the server writes the type in AMF. To do this set the « explicit type marker » on your returned object. Note that it must be an actual object, not an array. For example:


$returnObj = new stdClass();
$returnObj->_explicitType = "MyVO";

Note that with a packet sniffer that understands AMF like Charles Proxy you can check that the returned objects are typed. This is a good check before going on to getting it working client side.

If you want to use actual classes in PHP and want to set up class mapping, which is the only way to have strongly typed objects in Amfphp in versions 1.9 and earlier, please check the Vo Converter Plugin.

Client Side

Make sure your client knows what to with the type. If it doesn’t it will just become an « Object » in Flash for example. To do this you need an actual class, and you must make sure that it is registered so that Flash matches the AMF type with the client type. This is done with the registerClassAlias function.

Note that for strongly typed objects to work, your client class must not take any parameters for its constructor.

Flash

in Flash, define your class:


public class MyVO{
}

then call somewhere in application initialisation code:


registerClassAlias("MyVO", MyVO);
Flex

In Flex, the idea is the same, but you can use the RemoteClass metadata tag instead:


[RemoteClass(alias="MyVO")]
public class MyVO{
}
Javascript

As mentioned above, JSON doesn’t support object typing, but you can still set the “_explicitType” field in your data if you want to transmit the information to PHP.

Comments

  1. Marcello

    16 Juil 11

    This is not how 1.9 works, right?
    How is the simple way of keeping type working?

  2. ariels

    18 Juil 11

    no, it’s 2.0. There is no really simple way of getting strong typing as it’s a complex subject. If you have a vo folder it will work too, there’s a plugin for that called AmfphpCustomClassConverter. So your typing system as set up in 1.9 could work in 2.0.

  3. mutasembarjawi

    16 Oct 11

    I thought there was supposed to be something like a « $voPath » variable in which we set the path inside which amfPHP will look to match the incoming class from Flex applications… however, I cannot find this variable in AmfPHP 2.0… how amfphp will know where to find those vo classes?

    thanks

  4. ariels

    18 Oct 11

    @mutasembarjawi it’s in the AmfphpCustomClassConverter plugin.
    Set the customClassFolderPaths
    in your config.
    It defaults to Services/Vo

  5. mutasembarjawi

    20 Oct 11

    Thank you @ariels for your help. Yes I can find it now. However, as a best practice… and as mentioned on this website… that we shouldn’t modify those files as an update in the future might overwrite our changes.

    Is the proper way to change the vo folder is when creating my custom Config object, I assign a property like this:

    $config->customClassFolderPaths = « /my/custom/vo/path/ »;

    Finally, if it is a single path only, why the plural variable name?? can I set it to an array of vo paths?

    Thanks,

  6. ariels

    20 Oct 11

    The best is to create a custom config class as described in the doc.
    In the constructor, you need to add data to the pluginsConfig array that will be passed to the plugin.
    Something like :
    $paths = array(“/my/custom/vo/path/”);
    $customClassConverterConfig = array(‘customClassFolderPaths’ => $paths);
    $this->pluginsArray[‘AmfphpCustomClassConverter’] = $customClassConverterConfig;

    and it’s an array, so you must set it to an array, not a string.

Laissez un commentaire

Événements à venir / Upcoming Events

  1. Apéro de mars: Silex Labs #apéropensource #37

    30 mars @ 19:00 - 23:00
  2. Node (plus haut) dans les nuages

    5 avril @ 19:00 - 21:00

News letter

Evénements Silex Labs sur Paris et sa région: Inscrivez vous à la Newsletter mensuelle

Silex Labs community Tweets

Facebook page

Abonnez-vous aux news via Email

Rejoignez 41 autres abonnés