Re: Modular PHP

From: Terence (tk.lists_at_fastmail.fm)
Date: 03/16/04


Date: Tue, 16 Mar 2004 16:12:19 +1100

Hayden Kirk wrote:

> What im finding hard is the API
>
> Im use to Windows C++ Programming with API, this is much harded to do in
> PHP. How can I catch an event in PHP. Is there a way, like, if someone
> executes a function a module can pick that up and see what is happening,
> like in Windows?
>
> I have a basic outline of how I want to do this, but ill save you the time.
> I just can't get my head around making modules interact with my system that
> im going to design. I hope you get what I mean?
>
> Say if I print an invoice, I have an invoice module that someone else has
> created, can this pickup on that event?
>
> Thanks in advanced.
>

I'm doing some work on an opensource PHP framework which supports the
modularity you are speaking of. I haven't release the event-handling
component yet but if you are still interested, send me an email and I
will send you the source.

Everything is done with XML, so you will want to be familiar with XSLT
to use the framework (I detest proprietary templating systems).

Basically it listens for cirtain post(or other hash) variables that you
declare (these are the events) in an XML file which looks like so.

<?xml version="1.0" encoding="UTF-8"?>
<xao:RequestMap
xao:xmlns="http://xao-php.sourceforge.net/schema/xao_1-0.xsd">

        <xao:RequestSet ReqName="GameTasks">
                <xao:Request ReqValue="rounds" Handler="Handle_GT_rounds" />
                <xao:Request ReqValue="edit" Handler="Handle_GT_edit" />
                <xao:Request ReqValue="close" Handler="Handle_GT_close" />
                <xao:Request ReqValue="players" Handler="Handle_GT_players" />
                <xao:Request ReqValue="docs" Handler="Handle_GT_docs" />
                <xao:Request ReqValue="add" Handler="Handle_GT_add" />
        </xao:RequestSet>
        
</xao:RequestMap>

In the above example you might be delacring events for the variable
$_REQUEST["GameTasks"] but if you like, you could do

$arrRequests = $_POST;
or $arrRequests = $_REQUEST;
or $arrRequests = $_COOKIE;
or $arrRequests = $_POST["arrEvents"];
(where <select name="arrEvents[]" />)

It really doesn't matter where you get the hash from. The framework
checks the value of the "GameTasks" item (in this case) and then calls
the function itentified by the Handler attribute in the above XML,
passing the $arrRequests to it.

This is how it is called.
$objReqMap = new RequestMap("RequestMap.xml");
$objReqMap->ExecuteRequests(
        new HandleGameEdit(...),
        $arrRequests
);

In this case, the module would be the HandleGameEdit class which
contains methods named after those in the Handler attributes. Here's
what a module make look like...

class HandleGameEdit extends HandlerBase {

     function HandleGameEdit(&$objHost) {
         $this->HandlerBase($objHost);
     }

     function Handle_GE_update($arrReq) {
         $this->objHost->objLgDb->UpdateGame(
             $arrReq["game_id"],
             $arrReq["title"],
             $arrReq["start_ts"],
             $arrReq["end_ts"],
             $arrReq["summary"]
         );
     }
}

You could call ExecuteRequests multiple times, if you want to, with
different modules (object instances) which might implement an arbitary
selection of the declared event-handlers.

http://xao-php.sourceforge.net/
when it is finally released.

I admit that some poeple may find this complex, but then you wouldn't be
using the RequestMap feature unless you were writing a complex app!

It's also worth noting that all the XML parsing for the request map
config file is only done once, it then creates a cache file containing a
serialize()d version of the (internal) array generated by the request
map file. So performance is quite good.
To update the request map config file with new modules, simply edit it,
then delete the cache file. (I could probably do this automatically by
checking the timestamp but that's overhead for every single hit - I'm
still considdering it).

email me for the current source if you're keen.



Relevant Pages

  • Re: Object Oriented Content System - the idea
    ... >I expected php to be smarter then that. ... >form like bytecode in memory for the next request. ... each action added some stuff to a "response" XML document. ... that map/object/data and formats it for HTML display using an includeed PHP ...
    (comp.lang.php)
  • Re: How can I get the message-body ?
    ... > request will contain the XML as reqest-message body ... > How do I get the HTTP request message body? ... requests with arbitrary body data. ... PHP Classes - Free ready to use OOP components written in PHP ...
    (comp.lang.php)
  • Re: Send XML from PHP to C++ Server
    ... | Is it possible to send an XML request from PHP to a C++ XML Server. ... the request, but the request itself...aka. ...
    (alt.php)
  • Re: Securing an Email script
    ... request to our sales office. ... Since you do ZERO checking on the values it's nothing BUT security ... very powerful PHP function to validate form fields and other ...
    (comp.lang.php)
  • Re: Securing an Email script
    ... request to our sales office. ... Since you do ZERO checking on the values it's nothing BUT security issues. ... very powerful PHP function to validate form fields and other strings - ...
    (comp.lang.php)