What is the best way to hook a rewrite function for certain php code when plugins are updated?

There is no clean solution to this, because task is, well, dirty. 🙂 You should really be making these modifications in a proper ways.

If I absolutely had to do this (and even than probably for specific plugin and not at scale), I would look into maintaining my changes as vendor branch in version control and rebase on top of new releases. Even then it’s not really convenient or maintenance-light solution.