Thoughts on remoting

english mobile

The past week or so, I've been doing something that's new to me--retrieving objects using AMFPHP and integrating them with Flex. In the meantime, I've found that there are some things that are hard to find out about the whole process, so I thought I'd make a note of them here.

First, all the examples I could find easily used NetConnection. The code on these was so similar that I think there was one basic example that has been modified slightly by several different people and posted. I had better luck using RemoteObject, and I finally found an example of this.

The next thing I ran up against is that you have to use a RemoteClass alias on your Flex class that defines the object that is receiving the data. The docs are a bit misleading in that it sounds like you only need to use this if you are sending to a remote service and the service is Java based. However, if you read a bit further (I didn't at first), you can see that this applies pretty much to any AMF mapping. If you are sending data only from the service to Flex and you're not sending data back from Flex to the service, the only thing that matters about this alias is that it must match the $_explicitType variable in the object on the PHP side.

I also discovered a few things on my own. My classes extended EventDispatcher, and at first they didn't deserialize properly. We speculated that the fact that the class wasn't just a subclass of Object might have been the problem, but it turns out that extending EventDispatcher doesn't break serialization. My classes also had their own methods and some additional properties. We speculated that these were causing the problem, but it seems that serialization ignores these things. I think that if I were sending data back to PHP through the RemoteObject, I'd need to mark those properties as transient.

I think the real reason the serialization wasn't working was the last thing I discovered, which is that you have to have a dummy variable typed as your custom class in the same class as the RemoteObject that you're using to retrieve the values. This makes sure that the class definition gets compiled into the class where it can be used.

Oh, and one more thing... To a PHP programmer who's not familiar with ActionScript, a setter looks like something he can't serialize to. So it's probably better to just write up a description of the properties you're expecting to have set than sending the PHP person your actionscript class as is.

3 comments:

Abdul said...

Hi Amy,
Can you elaborate and possibly with an example of your paragraph "
I think the real reason the serialization wasn't working was the last thing I discovered, which is that you have to have a dummy variable typed as your custom class in the same class as the RemoteObject that you're using to retrieve the values. This makes sure that the class definition gets compiled into the class where it can be used."
I'm running into the same issue and not able to figure out what to do.

Amy B said...

If you look at http://flexdiary.magnoliamultimedia.com/HierarchicalRemoteObject/ you can see that there is a separate Class that handles loading the remote Objects. When I wrote this, I had a similar structure, but the result and fault handlers were inside that Class instead of passed in from outside. So inside that Class I needed to have the equivalent to a Classification VO.

In the implementation you see in the example, this is not needed because the fault and result handlers "live" in the main MXML file, which already knows about Classification, or in Classification itself, which of course also knows its own definition.

Siddharth Srivastava said...

Hi Amy,

I was forwarded to your post through a forum thread.

I am little new in flex. I use flash builder 4.5 for PHP. I have developed a flex and php app. All working fine but now it displays following error:
Send failed
Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 200: url: 'http://localhost/plasticphp/public/bin-debug/gateway.php'

I searched the net and found usually occurs in IE. But I am getting it on firefox and chrome.

You described serialization problem. Is it because of it.

Please guide.