Using PHPObject (Basic)
The Basics
The basic idea behind PHPObject is to link a local flashmx object with a remote php class, so that the methods of the php class can be called by flash. This allows you to exchange arrays, objects, strings, and other data types between client (FlashMX) and server (PHP) easily.
When a PHPObject is created, it 'inherits' its default properties from the remote php class. Subsequently the object can change these properties, and it can also have other properties declared within flash. When it calls a remote method, its properties are communicated to the server so that the remote class will have the same information as flash. Effectively, your flash object behaves as usual, except now it has an extended set of remote methods that it can call on the server.
The following is what you would typically enter in FlashMX:
////////////////////////////////
// Import the PHPObject.as file
////////////////////////////////
#include "PHPObject.as"
////////////////////////////////
// Configure
////////////////////////////////
// we set up the gateway first
_global.defaultGatewayUrl = "http://mydomain.com/services/Gateway.php";
// and the key
_global.defaultGatewayKey = "secret";
////////////////////////////////
// Creates the PHPObject
////////////////////////////////
myFoo = new PHPObject("Foo");
////////////////////////////////
// Event Handlers (Responders)
////////////////////////////////
myFoo.onInit = function() {
// here we define what to do after the object initializes
// upon initialization, the object is populated with the
// default properties of the remote class
trace(this.story);
}
myFoo.onResult = function(result) {
// here we define the default responder
// code to execute upon receiving result from server
// only executes if no specific responder was defined
if (typeof(result) == "object") {
for (var i in result) {
trace(i + ":" + result[i]);
}
} else {
trace(result);
}
}
myFoo.doRemoteMethod_onResult = function(result) {
// here is a specific responder that triggers when the
// doRemoteMethod is called and result received from server
// will not trigger default onResult handler
}
////////////////////////////////
// Invoke remote methods
////////////////////////////////
// call the remote method like you would for a local method!
myFoo.doRemoteMethod(param1,param2,...)
Getting data back from PHP
There are basically three ways to get data back from PHP. In your PHP method, if you used an output function such as echo, print (and similar functions), then in Flash, you will have to use PHPObject's getOutput() method to retrieve the data:
// php class
class Foo
{
function getResult($name)
{
echo "$name, you are a genius!";
}
}
// actionscript
myFoo = new PHPObject("Foo");
myFoo.getResult_onResult = function()
{
trace(this.getOutput());
}
myFoo.getResult("Einstein");
Alternatively, in the PHP method, you could use "return $variable", in which case you should retrieve the returned value as follows:
// php class
class Foo
{
function getResult($name)
{
return "$name, you are a genius!";
}
}
// actionscript
myFoo = new PHPObject("Foo");
myFoo.getResult_onResult = function(result)
{
trace(result);
}
myFoo.getResult("Frankenstein");
Or, in the PHP method, you could set a property, and retrieve the property like you normally would in an object in Flash:
// php class
class Foo
{
function getResult($name)
{
$this->result = "$name, you are a genius!";
}
}
// actionscript
myFoo = new PHPObject("Foo");
myFoo.getResult_onResult = function()
{
trace(this.result);
}
myFoo.getResult("You");
>> Documentation << |
|