monkey patching in php

前端 未结 5 1910
悲&欢浪女
悲&欢浪女 2021-01-05 03:49

I\'m trying to figure out how monkey patching works and how I can make it work on my own objects/methods.

I\'ve been looking at this lib, it does exactly what I want

5条回答
  •  无人及你
    2021-01-05 04:07

    I have monkey-patched a class using eval() and namespaces. This may not be the answer for you, though, since this does not work if the class you are monkey-patching is already in a namespace. I haven't figured out how to get around that, other than trimming the namespace declaration from the eval string. However, doing so would likely break any namespace-dependent code within the class methods.

    In my case, I am monkey-patching the core PDO class for unit-testing a class that depends on database interaction. But, maybe seeing my technique will help you figure out how to make it work for your situation.

    I have the code snippets up in a blog post here: http://chrisgriffing.com/coding/php/2012/04/12/how-to-mock-pdo-and-other-objects/

提交回复
热议问题