antecedent / patchwork

Method redefinition (monkey-patching) functionality for PHP.
https://antecedent.github.io/patchwork/
MIT License
444 stars 40 forks source link

How to alter a parameter before using Patchwork\pass() or before Patchwork\callOriginal()? #25

Open tomolimo opened 9 years ago

tomolimo commented 9 years ago

The idea is to have the possibility to alter a parameter before calling the original method. I found this working:

public static function patchworkMyFunction($type, $options) {
        if( $type == 3 ) {
            $param = Patchwork\Stack\top('args');
            $param[1]=-1;
        }
        return Patchwork\callOriginal();
    }

But not this

public static function patchworkMyFunction($type, $options) {
        if( $type == 3 ) {
            $options=-1;
        }
        return Patchwork\callOriginal();
    }

And not this:

public static function patchworkMyFunction($type, $options) {
        if( $type == 3 ) {
            $param = Patchwork\Stack\top('args');
            $param[1]=-1;
        }
        Patchwork\pass();
    }

Could you advise or comment? Thank you, Regards, Tomolimo

antecedent commented 9 years ago

Hello, I'm sure you have figured this one out by now, but for the future reference of other users, this can be done by passing an argument array to callOriginal:

public static function patchworkMyFunction($type, $options) {
        return Patchwork\callOriginal([-1, $options]);
}