IService.php
Este es la interfaz que tus clases deberian de implementar para tener un control de ellas.
<?php
interface IService{
function _response();
}
?>
GenericJson .php
En esta clase hacemos un ejemplo sencillo del uso de los servicios web.
<?php
require_once "Service.php";
class GenericJson implements IService{
public $_response;
public $_actionName;
public static $DEFAULT_ACTIONANE='default';
#---contructor
public function __construct(){
global $_SERVER;
global $_REQUEST;
$this->_actionName = array_key_exists('action',$_REQUEST)?
$_REQUEST['action'] :
$this->DEFAULT_ACTIONANE;
}
#---default action
public function default(){
return "--";
}
#metodo implementado
function _response(){
$action = $this->_actionName;
$res= $this->$action();
return $res;
}
#cuando la instancia de esta clase
#sea llamada como string, se llamara a este metodo.
function __toString(){return $this->response();}}
?>
Service.php
Este es tu bootstrap, el archivo a donde deberan apuntarse las llamadas.
<?php require_once 'GenericJson.php'; error_reporting(E_ALL); $instance=new GenericJson; die($instance); ?>
index.html
El archivo de presentación.
<html>
<head>
<script>
#se hace uso de jquery para este ejemplo#aunque bien funciona con otras librerias.
function _getServiceResponse(name,params,callback){
$.post('service.php?action='+name,params,callback,'json');
}
#
function _callback(result){
alert(result);
}
</script>
</head>
<body>
<input type="button" onClick="getServiceResponse('default',{name:'Jimmy'},_callback);">
</body>
</html>