1 Replies - 10917 Views - Last Post: 03 July 2012 - 07:43 AM

#1 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 175
  • Joined: 23-March 09

Decorator pattern

Posted 03 July 2012 - 03:46 AM

I was thinking of applying the decorator pattern at some place in my application and then discovered this might not be doable in a nice way in PHP.

I have a class with many public methods that I want to decorate. This class implements an interface enforcing the implementation of these public methods. My naive thinking was "I'll just create a decorator that uses __call to foreward the method calls to the object it holds". Sadly enough it looks like PHP does not understand this and complains about my decorator not implementing the methods defined in my interface. So it looks like I'm forced to foreward all methods (I'd really like to avoid this) or use another approach (I can't think of any sane one).

Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Decorator pattern

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Decorator pattern

Posted 03 July 2012 - 07:43 AM

if your decorator class implements the interface, it needs to explicitly declare the interface’s methods (or get them from an extending a suitable class). that’s how interfaces work. methods using __call() cannot be resolved through the interface (well, kind of natural. otherwise, how should a magic method adhere to an interface?). and I’m pretty sure every language would do the same.

however, you can extend the original class if you need the interface implementation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1