A pure PHP library that lets you redefine user-defined functions at runtime. Released under the terms of the MIT license.
In other words, Patchwork is a partial implementation of runkit_function_redefine
in userland PHP 5.3 code.
As of now, it only works with user-defined functions and methods, including static, final, and non-public ones.
Internal function redefinition functionality is currently only offered by core PHP extensions: Runkit, ext/test_helpers, and krakjoe/uopz.
It is, however, planned and being developed for Patchwork's next major release.
Patchwork requires at least either Zend's PHP 5.3.0 or HHVM 3.2.0 to run. Compatibility with lower versions of HHVM is possible, but has not been tested.
All these steps occur at the same runtime:
function size($x)
{
return count($x);
}
size(array(1, 2)); # => 2
Patchwork\replace("size", function($x)
{
return "huge";
});
size(array(1, 2)); # => "huge"
Patchwork\undoAll();
size(array(1, 2)); # => 2
To make the above example actually run, a dummy entry script is needed, one that would would first import Patchwork, and then the rest of the application:
require 'vendor/antecedent/patchwork/Patchwork.php';
require 'actualEntryScript.php';
Variations on this setup are possible: see the Setup section of the documentation for details.
For instance, PHPUnit users will most likely want to use a --bootstrap vendor/antecedent/patchwork/Patchwork.php
command line option.
A sample composer.json
importing only Patchwork would be as follows:
{
"require-dev": {
"antecedent/patchwork": "*"
}
}
For more information, please refer to the online documentation, which can be accessed and navigated using the top menu of Patchwork's website.
If you come across any bugs in Patchwork, please report them here. Thank you!