Using AMFPHP with PHP Classes

If you want to connect Flash to PHP code via AMFPHP, you need to write your PHP code as a PHP class. The remote methods that your ActionScript code calls are the methods of your custom classes. A PHP class uses the following syntax:

class MyClass {
  var local1 = 1;
  var local2 = 2;

  function MyClass ( ) {
    // do something
  }

  function someMethod ($val) {
    // do something else
  }

  function someOtherMethod ($val0, $val1) {
    // and something else
  }
}

In this case, local1 and local2 are instance variables (unique variables associated with each instance of this class). The function MyClass( ) is this class’s constructor, which runs when an instance of this class is first created. This method performs necessary setup for an instance of this class. Finally, someMethod( ) and someOtherMethod( ) are two sample methods to which all instances of this class have access.

For AMFPHP to work properly, each class must be defined in its own file and the file must have the same name (including capitalization) as the class. In addition, the class’s constructor must include a method table, specific code to define the signatures and permissions of all methods of the class. This information is used primarily by the Service Browser in Flash MX to help you write the correct ActionScript, but the permission information is used by AMFPHP to limit which methods of your class are accessible via Flash Remoting. This approach lets you define private methods that can be called internally ...

Get Flash Remoting: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.