2

I need to implement an observer design pattern in PHP. The problem with the standard SplObserver interface is that its update(SplSubject $subject) method doesn't support supplying additional arguments which is necessary for me.

Do I have to create my own interface like this?

interface PushObserver {
  public function update($sender, array $pushData);
}

Or is there a more standard way which wouldn't require users of my class to implement my own interface? I know I am not the only one dealing with this, but I haven't found a single article about this issue.

5
  • Without seeing the original code, could you not override the original method? Commented Sep 3, 2012 at 13:03
  • What about public members on the sender that the observers could use for additional info? Commented Sep 3, 2012 at 13:09
  • @Ben Can you be more concrete? I kinda don't know how I can override method in SplObserver in any nice way. Commented Sep 3, 2012 at 13:09
  • @Pinetree Thats the origin of my problem that I don't have these public members. I want to catch events on methods like insert(), update(), delete() and I need to pass data about how the request went. Commented Sep 3, 2012 at 13:13
  • Well, if you need just transient info which should not be a member of the sender, then I think you have no choice but using "non standard" custom classes. Having said that, I do not think there's anything wrong with that. Commented Sep 3, 2012 at 13:18

3 Answers 3

2

I don't know if it's really important to you to demand $pushData in your observer, but if it's only optional you can still implement or extend SplObserver, but make the $pushData optional, it lets you do that:

class PushObserver implements SplObserver {
    public function update (SplSubject $SplSubject, Array $pushData = null) {}
}

Or:

interface PushObserver extends SplObserver {
    public function update (SplSubject $SplSubject, Array $pushData = null);
}
9
  • I think PHP will complain if the update function does not have the same signature as the one in the SplObserver, but I can't remember if it's an error or just a warning. Commented Sep 3, 2012 at 13:20
  • I did test it out in PHP 5.4.3 before I posted this. PHP doesn't mind as long as additional arguments are only optional - 'cos the old signature will still work. Commented Sep 3, 2012 at 13:21
  • The problem here is that if I accessed instances of this class by interface (which I want). I wouldn't see the overloaded version. Commented Sep 3, 2012 at 13:22
  • @HonzaBrabec true, but does that matter? It still has an interface, and anyone who cares to look can see that your interface extends SplObserver... why is it important that it directly extend SplObserver? This pattern seems to be pretty neat OO practice to me. Commented Sep 3, 2012 at 13:24
  • 1
    OK looks like I have 2 choices. This one or the getPushData() method mentioned in the other answer. I personally prefer this one because the other one would pollute the public interface of my class with a method that IMO doesn't fit there. I will accept this answer if nothing else comes in a while Commented Sep 3, 2012 at 13:37
1

The sender knows about the pushData, right? Why bother creating a new interfaces if editing the sender could be enough?

Just add a getPushData() method to $sender to retrieve that variable.

1
  • 1
    This is a solution too, although I prefer the other one as stated in the comment below it. Anyway I gave you +1 for your effort Commented Sep 3, 2012 at 13:39
1

In your SplObserver, modify your update method to reflect:

public function update(SplSubject $subject, array $params = array())

and then use call_user_func_array to do the rest.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.