For our example, we need to create the static methods add() and
subtract() in a class named plgXMLRPCFoobarServices. It is normal to implement
these procedures within the same class as the event handler.
When we define the parameters for these methods, we must define the same number
of parameters as we did in the signatures. This example shows how we might
implement the add() and subtract() methods:
**
* Foobar XML-RPC service handler
*
* @static
*/
class plgXMLRPCFoobarServices
{
/**
* Adds values together
*
* @static
* @param float xmlrpcDouble
* @param float xmlrpcDouble
* @return xmlrpcresp xmlrpcDouble
*/
function add($value1, $value2)
{
global $xmlrpcDouble, $xmlrpcStruct;
// determine the sum of the two values
$product = $value1 + $value2;
// build the struct response
$result = new xmlrpcval(array(
'value1' => new xmlrpcval($value1, $xmlrpcDouble),
'value2' => new xmlrpcval($value2, $xmlrpcDouble),
'product' => new xmlrpcval($product, $xmlrpcDouble)
), $xmlrpcStruct);
// encapsulate the response value and return it
return new xmlrpcresp($result);
}
/**
* Subtracts a value from another
*
APIs and Web Services
[ 306 ]
* @static
* @param float xmlrpcDouble
* @param float xmlrpcDouble
* @return xmlrpcresp xmlrpcDouble
*/
function subtract($value1, $value2)
{
global $xmlrpcDouble, $xmlrpcStruct;
// determine the difference of the two values
$product = $value1 - $value2;
// build the struct response
$result = new xmlrpcval(array(
'value1' => new xmlrpcval($value1, $xmlrpcDouble),
'value2' => new xmlrpcval($value2, $xmlrpcDouble),
'product' => new xmlrpcval($product, $xmlrpcDouble)
), $xmlrpcStruct);
// encapsulate the response value and return it
return new xmlrpcresp($result);
}
}
The example introduces two classes that are fundamental to creating a response.
Pages:
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421