0

I already have a question regarding adding new methods to classes and it's already solved. However, my issue now is that when it comes to adding new methods to classes of libraries, I would have to readd the code every time I update the lib.

I already looked at solutions such as runkit_method_add and classkit_method_add, but they are not bundled in php by default.

What is the cleanest solution to my problem?

user3210615
  • 7
  • 1
  • 18
  • 1
    A clean approach would be some *specific* code where one could respond to :) – Jan Feb 08 '16 at 16:37
  • 3
    There isn't really any clean solution to your problem; though you might consider [Traits](http://www.php.net/manual/en/language.oop5.traits.php) – Mark Baker Feb 08 '16 at 16:38
  • I'd suggest using traits or using a decorator pattern, that wouldn't add methods to the specific class though. Another option would be to have a __call magic method in the class, with the possibility of adding closures to an array that is used in that __call method. – thisisboris Feb 08 '16 at 16:39
  • 1
    In what specific situation would you feel compelled to add something to an existing class? That's not typically a great idea. – deceze Feb 08 '16 at 16:40
  • Here is a file of the lib I'm using: https://github.com/waylaidwanderer/PHP-SteamCommunity/blob/master/TradeOffers/TradeOffer.php I would like to add a method here, which would return the state of the offer in strings. http://pastebin.com/CYzjSUbf – user3210615 Feb 08 '16 at 16:45
  • 4
    erm... `extend`, then add the methods in the child class? If a library doesn't provide a method you want, then either the library is flawed, or you're trying to do something project specific. In case of the latter: extending might sometimes be useful, but _wrapping_ is generally better. That is, of course, if you're not trying to do something the library was never intended to be used for... – Elias Van Ootegem Feb 08 '16 at 16:48
  • What do you mean by wrapping? google did not help me – user3210615 Feb 08 '16 at 16:49
  • `(new MyWrapper($originalClass))->someNewMagicMethod()` – deceze Feb 08 '16 at 16:50
  • 1
    @user3210615: Create a class that contains an instance of the library object you want to use. Add methods to that class (that wraps around the library instance), and put the code that interacts with the library object in there. Say you often need to perform 4 calls to a library object: create a wrapper with a single method that performs thsoe 4 calls and returns only the data you need... it's a common practice (google would've pointed you [here](http://stackoverflow.com/a/9371364/1230836)) to explain wrapping BTW – Elias Van Ootegem Feb 08 '16 at 16:51
  • Isn't this what inheritance and child classes are for. Create a child class that extends the relevant class in the library, and add the additional functionality. – Dan Feb 08 '16 at 16:53
  • 2
    @dan08: It's a library we're talking about. A rule of thumb here is to never extend objects you don't own, it might break your code next time you update. It also depends: if the methods you add breaks the SRP (single responsibility principle), then you should wrap, not extend. Lastly: inheritance chains should be as shallow as possible. Don't go `extends`-crazy if you can avoid it – Elias Van Ootegem Feb 08 '16 at 16:56
  • oh wow I understand. Thanks for all you guys. I will go with wrapping, it just feels more clean for me. Correct me if I'm wrong. Oh, and just to clear up things, decorating and wrapping are the same thing, right? – user3210615 Feb 08 '16 at 17:00
  • @user3210615 Not quite. Decorating (https://en.wikipedia.org/wiki/Decorator_pattern) is a more complex form of wrapping (and maybe not even best described by Wikipedia) – Opux Feb 08 '16 at 17:21

0 Answers0