What are Events?

What are Events?

An event is any occurrence to which your application can react. You will almost certainly have already written applications that have handled events in some capacity or other. When something happens or some condition is suddenly met, deciding what action to take in your code, no matter how trivial, is a form of event handling. For example, imagine that you have implemented a database holding a number of records, and you want your customers to be able to work on those records via a Web interface. You would probably realize that the users would need to at least be able to view and update records, so your likely reaction would be to create functions to view and edit records.

The decision of which function to call depends entirely on the input provided by the user. Accordingly, the interface you develop will need to present the various options and let the users decide which one they want. After the user has decided, the application needs to respond accordingly by calling the correct functions, which will handle the request.

Typically, you might create a home page with two buttons, one offering the option to view the records and another to edit them. The following code snippet might suffice as a mechanism to handle their choice:

   switch($_GET['action']) {      case "edit_record":        edit_record();      break;      case "view_record":        view_record();      break;    } 

As you can see, the previous snippet of code is designed to interpret information from $_GET (in other words, the user's request) and make a decision about what response to take, based on what the user has requested. That's it you have handled an event that was fired by the simple action of the user's clicking a button.

This principle stands for pretty much any event you could possibly imagine. For those of you who use Outlook or something similar, system-generated events are commonplace. Warnings about meetings and deadlines pop up all the time based on a specific event firing (for example, the system's clock reaching a certain time).

Of course, how you implement the code to handle events is up to you. Using a switch statement is not the only way; any sort of loop could allow you to respond to events. An if / else block would work just as well in the previous example.

"So what is all the fuss about events?" you may ask. After all, what we've seen so far seems simple enough; why can't we just leave it at that? The problem with what we have discussed so far is that you could easily end up with a big mess if your application changes or grows after your initial development run. What starts off as a nice simple if statement could grow to monstrous proportions, rendering any attempts to understand it in the future very difficult indeed. This has obvious implications as your application's needs become more complex. Accordingly, you should use the simple methods alluded to above only for very simple requirements, where you can be sure the code is at no time going to blossom into a full-blown enterprise application.



Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 182
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net