Is it possible to replace (monkeypatch) PHP functions?

前端 未结 6 774
感情败类
感情败类 2020-11-27 07:36

You can do this in Python, but is it possible in PHP?

>>> def a(): print 1
... 
>>> def a(): print 2
... 
>>> a()
2
6条回答
  •  爱一瞬间的悲伤
    2020-11-27 08:06

    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().

提交回复
热议问题