Are Promises/A a good event design pattern to implement even in synchronous languages like PHP?
Posted
by
Xeoncross
on Programmers
See other posts from Programmers
or by Xeoncross
Published on 2012-12-17T22:13:11Z
Indexed on
2012/12/17
23:13 UTC
Read the original article
Hit count: 209
I have always kept an eye out for event systems when writing code in scripting languages. Web applications have a history of allowing the user to add plugins and modules whenever needed.
In most PHP systems you have a global/singleton event object which all interested parties tie into and wait to be alerted to changes.
Event::on('event_name', $callback);
Recently more patterns like the observer have been used for things like jQuery.
$(el).on('event', callback);
Even PHP now has built in classes for it.
class Blog extends SplSubject {
public function save()
{
$this->notify();
}
}
Anyway, the Promises/A proposal has caught my eye. It is designed for asynchronous systems, but I'm wondering if it is also a good design to implement now that even synchronous languages like PHP are changing.
Combining Dependency Injection with Promises/A seems it might be the best combination for handling events currently.
© Programmers or respective owner