|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Event Handling Classes in PHP
Been looking at what Java and .NET do in regard to event handling in web applications.
To date, most PHP apps which get into OO use a fusebox like approach, in that one script (usually index.php) is used to handle events - all things begin with index.php. That's fine but I'm wondering if there's a more generic way to do it - a class you can simple instantiate from anywhere to handle events? Some resources for Java and .NET that discuss this; http://java.sun.com/docs/books/tutor...iew/event.html http://java.sun.com/docs/books/tutorial/uiswing/events/ http://www.devarticles.com/content.p...eId=172&page=1 The way I see it in PHP is something like this; An EventRegister class (using a Singleton pattern) which any object can use to register events with. An EventHandler class, which checks $_GET, $_POST, $_COOKIE and $_FILE for events registered with the EventRegister class. The next (tough) question is how the EventHandler can fire off the event itself (e.g. an object which validates incoming $_POST data then another object which INSERTs into a database). Any ideas? My attempts at writing the classes so far have left me deeply confused. |
|
|
|
|
|
#2 |
|
As the name suggests...
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Melbourne, Australia
Posts: 682
|
This could be interesting if you could make a way to pass an event queue to a script, there be having an unlimited order of events that can be passed.
If you could do this you could sort of extend the single request-response nature of a HTTP request. Maybe... I don't know... |
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Definately. Hadn't looked it that way but thinking about sessions and serialize objects, should be very possible.
I'll post the code I've been playing with later (don't have it to hand right now). |
|
|
|
|
|
#4 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
Harry,
I would not access the _POST _GET var directly but I will access them using a class. Something like the HttpRequest from Java. In my app I have something like: PHP Code:
About Event handling I had something similar on a C++ application that uses a GUI. There was an Event class which is the event itself. There was an EventManager class which was the queue of events. There was a class Object that have a method called Event. Every element on the screen had an Event method. So the code looked something like: // Create an event and put it in the queue eventManager->Put( new Event( L_SELECT ) ); // The event is passed to the object myObject->Event( eventManager->Get() ); It has been past many years since I used that app, so these are far memories. I have no idea if it can be of some utility for you, it's just a feedback from me. ![]() pippo |
|
|
|
|
|
#5 |
|
SitePoint Zealot
![]() ![]() Join Date: Dec 2001
Location: Ontario, Canada
Posts: 141
|
Harry: its ironic, I was thinking about doing something just like this, after a discussion I was having with Vincent on IRC a couple days ago.
I was actually thinking of how I could make my quick/dirty template system better, and more robust/OO ( its really just a quick hack right now )what I was thinking of doing, will work something like this: PHP Code:
![]()
__________________
Web Hound $x='010000010110001101101001011001000101001001100101011010010110011101101110'; for($i=0;$i<strlen($x);$i+=8)print(chr(bindec(substr($x,$i,8)))); Last edited by AcidReign; Oct 31, 2002 at 03:25.. |
|
|
|
|
|
#6 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
OK - got to rush - just splatting these (untested) classes down. Will explain more (handlers still required);
PHP Code:
PHP Code:
|
|
|
|
|
|
#7 |
|
Super Ninja Monkey
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 693
|
Another class for Pagoda?
![]()
__________________
Travis Watkins - Hyperactive Coder My Blog: Realist Anew Projects: Alacarte - Gnome Menu Editor |
|
|
|
|
|
#8 |
|
As the name suggests...
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Melbourne, Australia
Posts: 682
|
I really like this idea of an Application with a persistant state that responds to events.... NICE!
A also like your idea of Handlers being 'wrappers'. This could come in real handy to adapt current scripts into a sort of 'middleware' for an application. When i get some time i'd really like to explore this concept further... |
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
OK - first changing the trigger method in the listener class above;
PHP Code:
Here's my guess at how this might work; Two handlers; PHP Code:
PHP Code:
PHP Code:
Comments (including "You're soooo wrong" ) appreciated.Still taking in pippo and AcidReigns comments. Certainly there could be some more efficient approach using inheritance and polymorphism. Wanted to avoid having to change existing classes though to provide "AddEvent" methods - perhaps that's where polymorphism can come in... |
|
|
|
|
|
#10 |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Hey, no offense to your idea, but isn't this a lot easier?
Code:
<?php
switch ($_GET['formResponse']) {
case 'sendEmail':
mail ( $_POST['to'],$_POST['from'],$_POST['subject'];
print 'Email sent';
break;
default:
//whatever
}
1) PHP is a scripting language with minimal support for OO. And, being an interpreted, run-time language, the good old "less-is-more" mentality has to prevail. 2) It seems that truly useful event registers would have to be implemented by the application server (PHP engine, for instance) rather than wedged in by way of some overbearing PHP classes. 3) I'm a firm believer in "the right tools for the job", and with that said, there are plenty of scripted application servers for HTTP. Dumping HTML to a browser seems - at present - a trivial task, best handled in a simpler-is-better manner. If you're going to rethink the way things work (which is something I'm all for) you have to rethink the whole process. It doesn't make sense to reinvent one facet of app servers, only to shoe-horn it into existing methodologies. So in other words, why not a new architecture that implements HTML-inspired events straight out of the app server?
__________________
----Adopt-a-Sig---- Your message here! |
|
|
|
|
|
#11 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Alright!
![]() Well I agree with you on one level, although the example you give of a switch statement lacks some ingredients event handling could deliver such as the ability to have multiple events and not to be tied to expection particular named $_GET variables, for example. What if you have two important $_GET variables plus a cookie variable you want to response to? You're talking multiple switch statements or if/elses. I'm not saying I've got a perfect solution (probably far from it) but the idea is by wrapping up your code in a series of handlers, it could become extremely easily to reuse existing classes and handlers. All in all, it's a result of frustration from having built applications which ended up in giant and unwieldy conditional statements in index.php Quote:
Interesting point though, while you're suggesting application servers - http://sdk.ez.no/sdk/ezutils - the Module Handler for example and the HTTP Object Persistance classes... Been fooling with ezPublish alot recently, at work and at play - the current version handles events in a fairly crude way (looks for a particular filename then uses switch statements within that file) but is already excellent. You could already describe it as an application server in some senses. Version 3.0 is on the horizon and methinks it's gonna rock! But back to the point - if Javascript can have eventHandling, why shouldn't PHP? And more to the point, as it's being done in ASP.NET... |
|
|
|
|
|
|
#12 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 666
|
Quote:
- Multiple Inheritance Is considered a bad practice in OO. A form of multiple inheritance, interfaces, can be done in PHP. It just requires *your* (the programmer's) discipline - Private / Public Properties No property should be public anyway, it goes against encapsulation. The whole concept of private / public properties was only introduced to make it easier for programmers. Again, with discipline, this is not a problem. - Multiple constructors / function overloading Functions in PHP accept a variable number of parameters, so this can be done. - Static class variables Can be done by using a function with a static variable or by using globals which are hidden from the rest of the code (with your discipline of course). - Dereferencing ($obj->getObject->function()) Just use an extra variable, this is just something to make life a little easier, nothing more. - Destructors Those are IMHO merely for when you're lazy. With a little discipline, you can just call a destructor function instead of it being done automatically. My point is, with a little discipline, you can do almost *exactly* (correct me if I'm wrong) what you would do in a language like Java or C++ using objects. It all depends on how you treat your code. Of course, you can go and break your own rules like encapsulation by accessing private properties directly, but then this is your problem, not the language's problem. I believe voostind (can I get some support from you on this? ) once posted that even in languages like C++ it is possible to access an object's private properties.But we're going way off topic, so if you want to continue the PHP OO discussion feel free to start a discussion in a new thread or something. |
|
|
|
|
|
|
#13 |
|
SitePoint Zealot
![]() ![]() Join Date: Dec 2001
Location: Ontario, Canada
Posts: 141
|
alright, I've come up with another quick & dirty, shall we say, proof of concept
![]() since my code says more than I can: the classes the example now, what the example does not show, is that you do not need a template file ... you can call a [untested] show( $object ) method, that takes an object that is derived from DisplyHandler, show() then calls the display( $object ) method of the DisplayHandler, which is given the current Display instance [ $object->display( $this ); ] the Display handler can handle output in anyway it wants to ... it has access to all the values [setvalue/getvalue] and it can call the callevents() method as it wishes. comments? suggestions? flames?
__________________
Web Hound $x='010000010110001101101001011001000101001001100101011010010110011101101110'; for($i=0;$i<strlen($x);$i+=8)print(chr(bindec(substr($x,$i,8)))); |
|
|
|
|
|
#14 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
That looks excellent! Still taking it in but can see how you could use objects instead of templates. Really like the mechanism for adding events. Don't see right now how you respond to "user actions" though, such as index.php?view=page2.
Will have a play and see what happens... |
|
|
|
|
|
#15 | |
|
Ribbit...
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2001
Location: In your basement
Posts: 1,307
|
Quote:
1) Count number of arguments, and associate them with different functions: PHP Code:
__________________
Eric Coleman We're consentratin' on fallin' apart We were contenders, now throwin' the fight I just wanna believe, I just wanna believe in us |
|
|
|
|
|
|
#16 |
|
As the name suggests...
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Melbourne, Australia
Posts: 682
|
I'm thinking that this is definatley a good idea for an external extension.
Especially if persistance and object serialization is added. I also have been thinking that it would be cool if you could register events with a XML-RPC or some other remote call as the handler. AcidReign i like that code, but i think it would be better to abstract the event registering/executing stuff totally seperate from the display (although i can see how that could be a particular event) and then implement the presentation layer as events (although i already said that is kinda what you are doing) Anyway i think that if persistance is the name of the game then it would be good to do a C extension. |
|
|
|
|
|
#17 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
OK - finished of the "first version" based on what I started with (attached as Zip with a mini demo site included).
Here's how it can be used now; PHP Code:
Also it doesn't handle multiple events well. With the demo in the ZIP, if you use "index.php?view=links&action=addComment" for example, it will look a little wierd. Think the answer to that is rather than handlers echoing output directly, they put messages on an "event output queue" which the listener then deals with as the final step. Course when I start thinking about that, reckon this now is going too far. The other direction to go might be using output buffering and customer error handlers - use trigger_error to activate events. Might also be interesting to use get_declared_classes() in some way - perhaps to find all the declared handlers. Anyway - I need a break. This has all been too eventful ![]() Last edited by HarryF; Oct 23, 2002 at 02:22.. |
|
|
|
|
|
#18 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2002
Location: Estonia
Posts: 36
|
Can someone please explain the purpose of events in PHP with a few senteces, what problem they have to solve? I can understand their place in the GUI side of application (in JavaScript) but not in server side. Isn't the execution of the script only event needed (and only event that happens on server)?
Aivar Last edited by aivarannamaa; Oct 23, 2002 at 03:04.. |
|
|
|
|
|
#19 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Quote:
Here's one example; http://www.site.com/index.php - the home page http://www.site.com/index.php?view=news - site news http://www.site.com/index.php?action=login - the action of a "login form" http://www.site.com/index.php?update=userdetails - update your user details And what about this; http://www.site.com/index.php?view=news&action=login - login AND display the news To deal with this in the index.php, you're talking some big switch statements like; PHP Code:
The other approach to the problem is to specify particular variable names for example; http://www.site.com/index.php?application=forum&module=administration&event=deletepost That's fine in a way, but then all your code needs to conform to this standard - often a problem in intergrating old code an different applications. Also you're generally tied to a single script (index.php) being the "traffic cop" for your application. Last edited by HarryF; Oct 23, 2002 at 03:55.. |
|
|
|
|
|
|
#20 |
|
As the name suggests...
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Melbourne, Australia
Posts: 682
|
I also think that it is the order of events that can make this system eventful
. You could have your application respond to the an unlimited number of events in any order. Usually if you follow the 'old' system that Harry pointed out then the order of execution of a script is hard-coded (at least to some extent). This 'new' way you could get different responses by differing the order of event execution. |
|
|
|
|
|
#21 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
Harry,
I understand that what you called event I call command. That's because I'm implenting these concepts: http://www.martinfowler.com/isa/frontController.html Your works seems nice, but I want to point something. I see from your code that you use often: $_REQUEST[$eventname] Firs of all, why not use a class HttpRequest instead ?!? Is it not better to use something like $request->getParam( $eventname ); instead of $_REQUEST[$eventname] ?!? Your scripts will be more simple to mantain. Second thing, using $_REQUEST[$eventname] you are assuming that "events" are passed via a _POST or a _GET or whatever. But events could be passed to application by _REQUEST_URI using something like: /somepar1/someval1/somepar2/someval2 ( Okay here I could put some redirection rules ). Doing $_REQUEST[$eventname] I will have to rewrite the application on many parts. Would not be better to have a class that acts as an interface to the http requests and a class that acts as an event manager ?!? So the aim of the HttpRequest class is an interface to the http protocol requests. So I will read parameters as ->getParam(xx); and not as _GET[xx]. The "event manager", or whatever name you want to give it, will transform http requests ( that could be via GET, or via REQUEST_URI ) to events. I admit that I didn't read your code because I have not too much time available, so I'm sorry If I'm saying something wrong. I wanted only to share some thoughts. ![]() pippo |
|
|
|
|
|
#22 | |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Quote:
I won't be drawn into that argument here and now. Ignore my statements about OO support in PHP.
__________________
----Adopt-a-Sig---- Your message here! |
|
|
|
|
|
|
#23 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
|
Pippo - you're spot on and thanks for that link.
The use of $_REQUEST is bad - my first guess at how to solve the problem with PHP. |
|
|
|
|
|
#24 | |
|
As the name suggests...
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Melbourne, Australia
Posts: 682
|
Quote:
![]() And i agree that link pippo gave is cool. I think the java architecture has a good framework for this sort of thing. I've read that the architecture has good functionality like that, but because it generates so much overhead it slows the execution down. I dunno about that cause i don't know enough... Last edited by trickie; Oct 23, 2002 at 06:27.. |
|
|
|
|
|
|
#25 |
|
FreeBSD The Power to Serve
![]() Join Date: Jul 2001
Location: Italy
Posts: 4,568
|
I created just few days ago this class as interface to the http requests.
PHP Code:
but that was my approach to have an object to interface to the http protocol. I was trying to be inspired from what Java did. If you guys find that class interesting ( nothing fantascientific ) and you would want to expand it I would be happy .![]() pippo |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 09:25.









)
) once posted that even in languages like C++ it is possible to access an object's private properties.



Linear Mode
