[PEPr] Comment on RFC::NotificationCenter
| From: | Bertrand Mansion | Date: | Tue, 04 Jan 2005 12:17:42 +0000 |
| Subject: | [PEPr] Comment on RFC::NotificationCenter | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-35328@lists.php.net to get a copy of this message | ||
Bertrand Mansion (http://pear.php.net/user/mansion) has commented on the proposal for
RFC::NotificationCenter.
Comment:
Markus:
I have read your slides (me being flussig in German as you know ;)) and
obviously we are talking about the same thing. The main difference is the
implementation. Your design is more "complex", meaning you have more
classes, interfaces and instructions of use. The notification center I
propose registers PHP callbacks instead of objects because they are native
for PHP and do the job perfectly. IMO, there is no need to add a wrapper
around them. Their syntax allows for both methods and functions call,
which is a nice feature too as everything is still not object in PHP.
More importantly, PHP callbacks are a native structure, both well-known
and ready-to-use, which means users get it fast.
The other important difference is that ANY objects can post a
notification. It doesn't have to extend another class for that. Which
means the system is totally plug and play.
Lukas:
I have looked at the code you pointed out in LiveUser. This is of course a
solution but it is IMO not as handy as having a central notification
system. It forces developers to add and implement these methods (with a
risk of having different implementations, ie you use an Error_Stack while
others might trigger a warning or simply ignore the case where an event is
not registered). It also forces them to follow a way that might not suit
their needs.
I think these methods bloat to your objects. These 40 lines of codes
(repeated in every objects using observers) seems too much in regard of
the following:
<code>
// Registering for a notification
if (class_exists('NotificationCenter')) {
$nc = NotificationCenter::defaultCenter();
$nc->addObserver(array($this, 'userLogged'),
'UserDidLoginNotification');
}
// Posting a notification
if (class_exists('NotificationCenter')) {
$nc = NotificationCenter::defaultCenter();
$nc->postNotififcation($this, 'UserDidLoginNotification');
}
</code>
The NotificationCenter also allow some filters based on the class posting
the notification for example, or the name of the notification.
You can also have objects (for debug purpose for instance) that want to
listen to all notifications.
A notification can also be posted before the observer has registered, it
will still be notified if the user wants it to. You don't have to worry
about the order you instantiated your objects.
There could be more than one notification center (although this is not yet
implemented).
To finish with, there are cases when you want to give another object as
parameter than the one sending the notification, this is possible too.
Those are features not supported in your solution.
Alan:
The W3C events API you suggest looks very similar to the one I have. But I
feel strongly against forcing classes to extend an Event object. That's the
idea behind a notification center. In the end, I agree that it would be
nice to have something like this in PHP internals but it is not done yet.
In a way, it's like DB and PDO, PEAR_Error and Exceptions, etc. It is PHP4
(although a PHP5 version using filter iterators could be nice...).
Greg:
The notification center is a plug-and-play solution, you don't have to
modify your APIs and you are free to use it or not. Having a notification
center does not mean you can't have an interface to make your objects
"observable" in any other way but it is a lighter, centralized, solution
that is ready to use.
I am not sure I understand your 2-way messaging thing. I wrap the
notifications in a Notification object in order to provide a common API to
users so that they know what to expect from objects they might not know
well. The Notification class can also be easily extended and the subclass
can be used by $nc->post($notificationSubclass);
In the package documentation, users will only have to tell which kind of
notifications (their name) are posted by the object and when.
Proposal information:
http://pear.php.net/pepr/pepr-proposal-show.php?id=186
--
Sent by PEPr, the automatic proposal system at http://pear.php.net