Event-Based AJAX Framework for PHP : Part 4

Posted on June 4, 2008
Filed Under AJAX, JavaScript, PHP |

Articles in this series:

  1. Introduction to the Framework
  2. Problems with Creating an Event Based AJAX Callback Mechanism
  3. JSON data passing
  4. The Scriptifiable Interface
  5. The Remotable Interface

This is the fourth part to the PHP AJAX Framework series. In this article, I will explain how you can take the built-in JSON encoding one step further by implementing the Scriptifiable interface.

Isn’t JSON enough?

JSON is great. It’s simple and elegant. It lets you pass data back and forth from the server in a way that is easily parsed, but in the end, it’s just a data structure. There is no functionality that you can leverage outside of key:value pairs. The Scriptifiable interface attempts to address this issue by providing a structure for adding JavaScript to your PHP file that will be added to your parsed JSON object allowing you to keep all of your object code in one file for both PHP and JavaScript.

How do I implement it?

Just like implementing any interface, we start off by creating a class. Below is an example of a class (TestClass) implementing an interface (TestInterface):

 class TestClass implements TestInterface {

}

That’s it. All an interface is is a contract that the class that implements it promises to abide by. If the interface says there will be a method foo that returns a value of type bar, the implementing class must provide that method; even if it doesn’t do anything. The Scriptifiable interface is very straight forward and only requires one method: GetScriptClassMethods.

Since PHP is a weakly typed language (and one that doesn’t really offer much as far as type hinting either - though that’s getting better), the interface can’t require that GetScriptClassMethods returns a string, but that’s the idea. GetSCriptClassMethods must return a string that contains the JavaScript that will be added to the client-side class.

If your client performs a RemoteInvoke on TestClass::ServerSideFunction and that function returns a value of type ReturnClass, JSON is used to first get all public properties in ReturnClass and generate a JavaScript equivalent. After that is done, a method called populate is added to the client side version of the returned object. The populate method contains the string returned from GetScriptClassMethods and it is called when the server returns the value to the client (on page load).

Here’s an example of GetScriptClassMethods:

public static function GetScriptClassMethods() {

   return <<<JSCODE
   this.tellAStory = function() {

      alert("what is your problem. " + this.value);
   }
JSCODE;
}

The <<<JSCODE   JSCODE; is just a way of writing strings in PHP called heredoc. Heredoc does parse values (like this.value above) unlike nowdoc.

Get the Source

Download the PHP-AJAX Framework

What’s next?

In the next article in this series, I will discuss the last component to this Framework: the Remotable interface. With this, you will be able to designate which classes have static methods available for Remote Invocation, providing a simple level of protection from attack. (Don’t rely on this one, though.)

Comments

Leave a Reply




Captcha
← Enter these letters.