Don't do this! It's only curiosity that drove me to try.
A monkey patch is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, etc.) without altering the original source code. -- Wikipedia
Till tried to do some monkey patching a while ago, using PHP namespaces. Basically, if you use a PHP function inside a namespace, the original function will be called if an override is not defined. In the example, calling strlen() inside of a namespace would refer to the NamespaceName\strlen() function, and if it not exists, it would fall back to PHP's native one.
The problem was that you actually have to namespace the original code. It would not be monkey patching anymore: if I could modify that code, I would simply change the function calls.
For example, we may want to monkey patch the following code:
<?php
$str = 'mystring';
echo "This should eight, but it's not: " . strlen($str) . "\n"; // 6
So what do we do? Basically, this evil black magic:
<?php
namespace monkeypatching;
function strlen()
{
return 6;
}
$code = file_get_contents('monkeypatched.php');
$code = 'namespace monkeypatching; ?> ' . $code;
echo $code;
eval($code);
which prints out:
This should eight, but it's not: 6
Of course this infringes every single SOLID and software engineering principle. Again, don't code like this,
we are not in Ruby.
In general, methods and functions definitions are part of the mandatory global state of an application, and so once the loading phase has finished they should be immutable. This is done at the loading time, so it may be acceptable. But also dangerous: it's the poor man's version of polymorphism.
Don't do this! It's only curiosity that drove me to try.
A monkey patch is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, etc.) without altering the original source code. -- Wikipedia
Till tried to do some monkey patching a while ago, using PHP namespaces. Basically, if you use a PHP function inside a namespace, the original function will be called if an override is not defined. In the example, calling strlen() inside of a namespace would refer to the NamespaceName\strlen() function, and if it not exists, it would fall back to PHP's native one.
The problem was that you actually have to namespace the original code. It would not be monkey patching anymore: if I could modify that code, I would simply change the function calls.
For example, we may want to monkey patch the following code:
<?php
$str = 'mystring';
echo "This should eight, but it's not: " . strlen($str) . "\n"; // 6
So what do we do? Basically, this evil black magic:
<?php
namespace monkeypatching;
function strlen()
{
return 6;
}
$code = file_get_contents('monkeypatched.php');
$code = 'namespace monkeypatching; ?> ' . $code;
echo $code;
eval($code);
which prints out:
This should eight, but it's not: 6
Of course this infringes every single SOLID and software engineering principle. Again, don't code like this,
we are not in Ruby.
In general, methods and functions definitions are part of the mandatory global state of an application, and so once the loading phase has finished they should be immutable. This is done at the loading time, so it may be acceptable. But also dangerous: it's the poor man's version of polymorphism.
Monkey patching in PHP