Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

PHP - Dynamic Request Resolving

download example
view example

If we work with php or any other server-side-language, we mostly
communicate with client through get- or post-requests. While i was
searching on the php.net-site i found an interesting idea there and build a class based on that.

The Idea is to dynamicly resolve request-variables to method-calls. This is easy to realize because we have in php also the possibility to call an method dynamicly. Therefore exists in php the function call_user_func.

It would be not so secure to resolve a request-variable to any function in the current script. So I decided that an extra request-class is needed in which we allowed to resolve and execute any method. Like a sandbox.

Example-Usage: <?php

// Include the request-resolve
// class
require_once 'Request.php';

// the class which handles
// our requests
class RequestResolver
{
   // declare a receiver-method
   function sayHello ($param)
   {
      echo 'Hello '. $param .'<br />';
   }

   // declare another receiver-method
   function sayGoodbye ($param)
   {
      echo 'Goodbye '. $param .'<br />';
   }

   // declare another receiver-method
   function divideBy2 ($param)
   {
      echo $param / 2;
   }
}

?>

<html>
<head><title>Request.php</title></head>
<body>

<h4>Result:</h4>

<?php

// start resolving requests,
// with our RequestResolver-class
// as context
Request::resolve(new RequestResolver);

?>


<h4>Test get-requests</h4>

<a href="<?php basename(__FILE__) ?>?sayHello=world">sayHello(world)</a><br />
<a href="<?php basename(__FILE__) ?>?sayGoodbye=world">sayGoodbye(world)</a><br />


<h4>Test post-request</h4>

<form action="<?php basename(__FILE__) ?>" method="post">
<label>divideBy2:</label>
<input type="text" name="divideBy2" /><br />
<input type="submit" />
</form>


</body>
</html>

Posted by hOk at January 3, 2004 01:27 PM

Comments (0)


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place