Is it possible to replace (monkeypatch) PHP functions?

前端 未结 6 765
感情败类
感情败类 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:00

    No, it is not possible to do this as you might expect.

    From the manual:

    PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

    HOWEVER, You can use runkit_function_redefine and its cousins, but it is definitely not very elegant...

    You can also use create_function to do something like this:

    
    

    As with runkit, it is not very elegant, but it gives the behavior you are looking for.

提交回复
热议问题