0

I'm using a Wordpress plugin that has a php object with public methods that build HTML content for events, but I'd like to change one of the methods without directly changing the plugin so it can still be updated with newer versions without issue. Is there a way to reassign or overwrite an objects method from the function.php file, there's no filters or function_exists in their code. I know I can do this client-side in JavaScript or use jQuery.extend. I'm thinking it isn't possible in server-side, but thought I'd check.

Cheers

mtpultz
  • 17,267
  • 22
  • 122
  • 201
  • What about class inheritance (extends) and redeclare that method in the child class? – php-dev Jan 26 '14 at 07:09
  • possible duplicate of [Is it possible to overwrite a function in PHP](http://stackoverflow.com/questions/3620659/is-it-possible-to-overwrite-a-function-in-php) – Hardik Sondagar Jan 26 '14 at 07:10

2 Answers2

1

If the method you want to redeclare is NOT declared as FINAL and some methods/properties of the class are not PRIVATE (but, for example PROTECTED, or, even better, all PUBLIC) - use objects inheritance.

class YourNewClass extends stubbornClass
{
   public function stubbornMethod ($params)
   {
       echo "do whatever you want, but retain $params and return value matching original class";
       echo "you can also run the parent method version as:";
       parent::stubbornMethod ($params);
   }
}
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24
0

Your only option is create your custom plugin and reuse the code from the old one.

This is because even if you create a custom PHP class overriding the original one you can't tell the plugin to use your new class without change the old plugin lines where the object is created changing the old class name to your new class name.

Maks3w
  • 6,014
  • 6
  • 37
  • 42