I am starting to use decorators in PHP more often these days to modify an object’s behavior at run-time. My problem is primarily one of laziness, we have many legacy classes with tons of methods and the thought of having to re-write/override all of them for each decorator class makes me sad. Does anyone know of a command line utility that exists that would write these decorators for me?
Or maybe there’s a better way to go about this?
From the question I understand you are too lazy to add the other methods, e.g. those that do not modify the decorated instance. For this purpose, you can use use the magic method
__callYou can also add
__callStatic,__getand__setas needed. But note that the magic interceptors always incur some performance penalty. If you have a lot of nested decorators, this might be noticable. When in doubt, benchmark.