Fork me on GitHub
function size($x)
{
    return count($x);
}

size(array(1, 2)); # => 2
                    

1. Define a function

use Patchwork as p;

p\replace("size", function($x)
{
    return "huge";
});

size(array(1, 2)); # => "huge"

2. Replace its definition

p\undoAll();

size(array(1, 2)); # => 2

# Note that all of these steps
# occur at the same runtime!

3. Undo the redefinition

Attention!

We advise you to always think twice before using Patchwork for unit testing. While it does provide an alternate way to achieve testability, achieving it the conventional way, that is, by refactoring your code to introduce a looser coupling of components, often results in better general quality of code as well. We suggest only considering using Patchwork in cases where refactoring code for natural testability proves unreasonably complex.

Attention!

It is strongly recommended to limit the use of Patchwork to testing and development environments only. There are several reasons for that: firstly, Patchwork was never intended for use in production, which means it could have some undiscovered security concerns; secondly, redefined functions can quickly make code unmaintainable; and lastly, it will very likely have a noticeable impact on any application's performance.

Further Reading

Patchwork's documentation can be navigated using the top menu. If you have a question about Patchwork that the documentation doesn't answer, please don't hesitate to leave a comment on the documentation pages.

Reporting Issues

If you encounter any Patchwork-related problems on which the documentation is of no help, please review the currently open issues on Patchwork's GitHub site, and if you find none that would explain your problem, please open a new one.