AMFPHP, PHP 5.3 and namespaces

I spent a very frustrating day discovering that AMFPHP (trunk) doesn’t support class mapping to PHP classes that exist in the brand new PHP 5.3 namespaces.  So, if you happen to have the following class mapping:

   1: namespace vo;

   2:  

   3: class User {

   4:     $_explicitType = "vo.User";

   5: }

   1: package vo;

   2:  

   3: [RemoteClass(alias="vo.User")]

   4: class User {

   5:  

   6: }

… AMFPHP won’t pick up the fact that these are supposed to be the same class and will create an associative array when you send User as a parameter to a service.  It works fine in the opposite direction (i.e. returning User back to AS3 is ok).

I tried out ZendAMF and WebORB for PHP, but eventually decided that whilst they definitely have their merits, these frameworks are way too heavyweight for my application; ZendAMF requires the whole Zend Framework stack in order to work and WebORB is 22MB!  Instead I set about patching AMFPHP to support namespaces.  And here it is!

In amfphp/core/amf/io/AMFBaseSerializer.php change line 392 to:

   1: $classname = (strstr($typeIdentifier, "/")) ? str_replace("/", "", $typeIdentifier) : substr($mappedClass, $lastPlace);

Then if you want to class map to a PHP class in a namespace instead of using . notation, put forward slashes (/).  In fact PHP in its infinite wisdom uses backslashes () to delimit namespaces but as these denote escape characters terrible things happen when we start serializing strings and rather than have to escape the backslash all the time I just plumped for the forward slash.  Therefore our example, which in PHP namespace notation refers to the class voUser would change to:

   1: namespace vo;

   2:  

   3: class User {

   4:     $_explicitType = "/vo/User";

   5: }

… and our AS3 class would become:

   1: package vo;

   2:  

   3: [RemoteClass(alias="/vo/User")]

   4: class User {

   5:  

   6: }

Tada!  AS3 to PHP class mapping working as expected again!

4 comments

  1. Hi Dave,
    what is your amfphp version? I have amfphp-1.9.beta.20080120 and the line 392 in the AMFBaseSerializer.php file don’t exist, can you help me?

    Thanks!

  2. Hey Tim,

    I’m afraid that I recently switched over to ZendAMF and no longer have the code, so I’m not sure what version it was now. I seem to remember it was one of the 1.9 versions, but couldn’t swear to it. If you fancy a change I would now actually recommend ZendAMF as it no longer requires the whole framework (there is a download available that only has the required bits), and it both supports PHP 5.3 namespaces and has a nicer system for class mapping.

    Some guys are currently working on a rewritten AMFPHP 2 with all kinds of great features at which point it might be worthwhile to switch back, but for the moment ZendAMF is probably your best bet.

    Good luck!

    Dave

  3. @TIMNHE

    The file you should edit is not AMFBaseSerializer.php, its AMFBaseDeserializer.php. I also had to edit a couple of lines above for it to work.

Leave a Reply

Your email address will not be published. Required fields are marked *