Changeset 19856
- Timestamp:
- 07/03/09 19:14:58 (4 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
components/event_dispatcher/trunk/doc/01-Event-Dispatcher.markdown
r19844 r19856 2 2 =============================== 3 3 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 4 Objected Oriented code has gone a long way to ensuring code written 5 for your projects is extensible. By creating classes that have well 6 defined responsibilities, your code becomes more flexible. 7 8 If a user wants to modify a class's behavior, he can extend it using 9 a subclass to override the behaviour. But if the user want to share 10 his changes with other users who have made their own subclasses to 11 change the behaviour, code inheritance is moot. 12 13 A real-world example is when you want to provide a plugin system for 12 14 your class. A plugin should be able to add methods, or do something 13 15 before or after a method is executed, without interfering with other 14 plugins work. That's tougher to resolve. 16 plugins. This is not an easy problem to solve with single 17 inheritance, and multiple inheritance (were it possible with PHP) 18 has its own drawbacks. 15 19 16 20 Enter Symfony Event Dispatcher. The library implements the … … 55 59 56 60 As you might have noticed, event names contain a verb to indicate 57 that it relatesto something that happens.61 that they relate to something that happens. 58 62 59 63 The Dispatcher 60 64 -------------- 61 65 62 The dispatcher is the object responsible for connecting the66 The dispatcher is the object responsible for maintaining a register of 63 67 listeners and calling them whenever an event is notified. 64 68 … … 74 78 notified event. Its constructor takes three arguments: 75 79 76 * The *subject* of the event (most of the time, this is the object notifying77 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`); 78 82 79 83 * The event name; … … 105 109 -------------------- 106 110 107 Obviously, you need to connect some listeners to the dispatcher to108 make ituseful. A call to the dispatcher `connect()` method111 Obviously, you need to connect some listeners to the dispatcher before 112 it can be useful. A call to the dispatcher `connect()` method 109 113 associates a PHP callable to an event. 110 114 … … 125 129 $dispatcher->connect('user.change_culture', $callable); 126 130 127 Once a listener is registered inthe event dispatcher, it waits131 Once a listener is registered with the event dispatcher, it waits 128 132 until the event is notified. The event dispatcher keeps a record of 129 133 all event listeners, and knows which ones to call when an event is … … 279 283 if ('bar' != $event['method']) 280 284 { 281 // let the opport nuity to another listener to take care of this unknown method285 // let the opportunity to another listener to take care of this unknown method 282 286 return false; 283 287 } … … 307 311 ### Modifying Arguments 308 312 309 If you want to allow third party classes to modify the arguments310 passed to a method, just before it is executed, add a `filter` event 311 at the beginning of the method:313 If you want to allow third party classes to modify arguments passed 314 to a method just before that method is executed, add a `filter` 315 event at the beginning of the method: 312 316 313 317 [php]