You can do this in Python, but is it possible in PHP?
>>> def a(): print 1
...
>>> def a(): print 2
...
>>> a()
2
As jmikola mentioned, Patchwork is a good solution if you want to add code to a function.
Here's an article about how it works: http://phpmyweb.net/2012/04/26/write-an-awesome-plugin-system-in-php/
It comes with some sample code. I think the phpmyweb version uses a slightly better code, because he doesn't use eval()'d code, unlike patchwork. You can cache opcodes when using eval().