Event-Based AJAX Framework for PHP : Part 3
Posted on June 1, 2008
Filed Under AJAX, JavaScript, PHP | Leave a Comment
Articles in this series:
- Introduction to the Framework
- Problems with Creating an Event Based AJAX Callback Mechanism
- JSON data passing
- The Scriptifiable Interface
- The Remotable Interface
This is the third part to the PHP AJAX Framework series. In this article I will talk about how JSON was used in this Framework to pass complex data structures between the client and the server.
What is JSON?
JSON is a format developed by Douglas Crockford of Yahoo. The acronym is short for JavaScript Object Notation. The format is fairly simple and involves string : value pairs or basic ordered list of values. JSON does not allow you to pass methods between the client and the server (that would be hard), but this Framework provides a workaround for that scenario by having your PHP class implement the Scriptifiable interface. I’ll get to that later.
Client to Server
In the last article in this series, I talked about how the RemoteInvoke method worked and explained how you pass parameters to your server-side function in an array. What I didn’t mention is that each of those parameters are stringified to JSON before they are passed to the server. This is critical because it opens the types of parameters you can pass to custom javascript objects. If your server-side function needs all the information that’s contained in your JavaScript class to do the necessary processing, instead of passing each value individually, just pass the object itself. Then, on the PHP side, Remotee.php json_decode’s each parameter and passes them into the called function.
Server to Client
The same goes for return values. When your PHP is done executing, the return value is json_encoded on the server and then parsed on the client into a JavaScript object.
Get the Source
Download the PHP-AJAX Framework
What’s next?
In the next article in this series, I’ll explain how to take this one step further and not only return the member variables of your PHP classes, but methods as well by implementing the Scriptifiable interface.
Comments
Leave a Reply