This EventManager class is for PHP 5.3. It has got the same features as Yet Another EventManager Class in PHP (<5.3):
- EventManager is a Singleton
- EventManager events are strings
- EventManager bind() can attach handlers to regular expressions
- EventManager event handler arguments are loosely structured
- EventManager event handler return value is loosely structured
- EventManager gathers, stores, and eventually returns all handlers results
plus the following:
- EventManager belongs to the AndoCore namespace
- EventManager event handlers can be any PHP callable expression
- EventManager bind() / unbind() work with Callable objects
EventManager belongs to the AndoCore namespace
As a namespace for my projects I chose Ando, which is short, and tells a bit about me (Andrea), and a bit about Miho, my wife, which once called me Andorea.
EventManager event handlers can be any PHP callable expression
Global functions, class methods, object methods, and anonymous functions are all supported.
EventManager bind() / unbind() work with Callable objects
Not only bind() accepts any callable expression, but also accepts and returns an object of the class AndoCoreCallable. This way, for unbinding an event handler, the Callable object that bind() returns must be passed to unbind().
Callable objects are just a wrapper around callable expressions, that allows for a uniform access and storage.
{[ .callable | 1.hilite(=php=) ]}
—
How to use many EventManager singletons at once
{[ .another_event_manager | 1.hilite(=php=) ]}
As is, the previous scripts outputs nothing, but uncommenting the commented line you get
Hello Popeye !
Action Wrapping
{[ .wrapping_actions | 1.hilite(=php,ln-1=) ]}
Trace
2009-11-16 07:17:29: one:before: 3
2009-11-16 07:17:32: one:after: NULL
2009-11-16 07:17:32: one:before: 3
2009-11-16 07:17:35: one:after: NULL
2009-11-16 07:17:35: two:before: 2
2009-11-16 07:17:37: two:after: NULL
2009-11-16 07:17:37: two:before: 2
2009-11-16 07:17:39: two:after: NULL
2009-11-16 07:17:39: one:before: 3
2009-11-16 07:17:42: one:after: NULL
Runtime Class Extension
{[ .runtime_class_extension | 1.hilite(=php,ln-1=) ]}
Trace
{[ .runtime_class_extension_result /precode.php ]}
Filters Pipeline
{[ .filters_pipeline | 1.hilite(=php,ln-1=) ]}
Output
a *quick* red fox jumps OVER a _lazy_ brown dog.
a *quick* red fox jumps over a _lazy_ brown dog.
—
EventManager class
Last but not least, here is the code of the EventManager class
{[ .event_manager | 1.hilite(=php,ln-1=) ]}