What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated May 22, 2007 by janko.itm
Labels: Phase-Implementation, RPC, EXAMPLE
NumbersStringsArraysExample  

This is small example of exposing 3 methods on the STRPC server... you can test this interactively by visiting this page

require_once '../STRPC.php';

//create class instance
$basic = new Basic();

$map = array(
	array('basic', 'echoBack',  //module name and procedure (function) name
		array(
			array('text', 'string'), //argument name and type
		),
		'string' //return value type
	),
	array('basic', 'add',		
		array(
			array('num1', 'number'),
			array('num2', 'number'),
		),
		'number'
	),
	array('basic', 'sumAndCount',		
		array(
			array('nums', 'array'),	//some arrays
		),
		'arrays'
	),
);

$rpc = new STRPC();
echo $rpc->serve($map);

class Basic
//as you see this is totally ordinary class 
//and has no STRPC specific code
{
	function echoBack($t) { return $t; }
	function add($num1, $num2) { return $num1 + $num2; }
	function sumAndCount($nums) 
	{
		$sum = 0;
		$count = 0;
		echo 'we are doing some debugging here : ';
		print_r($nums); //some "debugging" helpers
		foreach ($nums as $num)
		{
			$sum += $num;
			$count ++;
			echo "<br/>adding $num, sum now is $sum, count is $count";
		}
		echo '<br/>(result is still valid)';
		return array('sum'=>$sum, 'count'=>$count, 'nested data'=>array('works nice'=>$nums, 'and displays nice'=>$nums));
	}
}

Sign in to add a comment