Development

Changeset 19856

You must first sign up to be able to contribute.

Changeset 19856

Show
Ignore:
Timestamp:
07/03/09 19:14:58 (4 years ago)
Author:
fabien
Message:

[event_dispatcher] enhancements to Event Dispatcher documentation (closes #6744 - patch from pbowyer)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • components/event_dispatcher/trunk/doc/01-Event-Dispatcher.markdown

    r19844 r19856  
    22=============================== 
    33 
    4 Objected Oriented code can come a long way in ensuring extensibility 
    5 of your projects. By creating classes that have well defined 
    6 responsibilities, you make your code more flexible. 
    7  
    8 If a user of your class want to modify its behavior, he can extend 
    9 the class. But if the user want to share its changes with other 
    10 users that also have their own changes, inheritance is moot. That's 
    11 the case for instance when you want to provide a plugin system for 
     4Objected Oriented code has gone a long way to ensuring code written 
     5for your projects is extensible. By creating classes that have well 
     6defined responsibilities, your code becomes more flexible. 
     7 
     8If a user wants to modify a class's behavior, he can extend it using 
     9a subclass to override the behaviour. But if the user want to share 
     10his changes with other users who have made their own subclasses to 
     11change the behaviour, code inheritance is moot. 
     12 
     13A real-world example is when you want to provide a plugin system for 
    1214your class. A plugin should be able to add methods, or do something 
    1315before or after a method is executed, without interfering with other 
    14 plugins work. That's tougher to resolve. 
     16plugins. This is not an easy problem to solve with single 
     17inheritance, and multiple inheritance (were it possible with PHP) 
     18has its own drawbacks. 
    1519 
    1620Enter Symfony Event Dispatcher. The library implements the 
     
    5559 
    5660As you might have noticed, event names contain a verb to indicate 
    57 that it relates to something that happens. 
     61that they relate to something that happens. 
    5862 
    5963The Dispatcher 
    6064-------------- 
    6165 
    62 The dispatcher is the object responsible for connecting the 
     66The dispatcher is the object responsible for maintaining a register of 
    6367listeners and calling them whenever an event is notified. 
    6468 
     
    7478notified event. Its constructor takes three arguments: 
    7579 
    76   * The *subject* of the event (most of the time, this is the object notifying 
    77     the event, but it can also be `null`); 
     80  * The *subject* of the event (most of the time, this is the object 
     81    notifying the event, but it can also be `null`); 
    7882 
    7983  * The event name; 
     
    105109-------------------- 
    106110 
    107 Obviously, you need to connect some listeners to the dispatcher to 
    108 make it useful. A call to the dispatcher `connect()` method 
     111Obviously, you need to connect some listeners to the dispatcher before 
     112it can be useful. A call to the dispatcher `connect()` method 
    109113associates a PHP callable to an event. 
    110114 
     
    125129    $dispatcher->connect('user.change_culture', $callable); 
    126130 
    127 Once a listener is registered in the event dispatcher, it waits 
     131Once a listener is registered with the event dispatcher, it waits 
    128132until the event is notified. The event dispatcher keeps a record of 
    129133all event listeners, and knows which ones to call when an event is 
     
    279283        if ('bar' != $event['method']) 
    280284        { 
    281           // let the opportnuity to another listener to take care of this unknown method 
     285          // let the opportunity to another listener to take care of this unknown method 
    282286          return false; 
    283287        } 
     
    307311### Modifying Arguments 
    308312 
    309 If you want to allow third party classes to modify the arguments 
    310 passed to a method, just before it is executed, add a `filter` event 
    311 at the beginning of the method: 
     313If you want to allow third party classes to modify arguments passed 
     314to a method just before that method is executed, add a `filter` 
     315event at the beginning of the method: 
    312316 
    313317    [php]