Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefine Class Methods or Class

Is there any way to redefine a class or some of its methods without using typical inheritance? For example:

class third_party_library {     function buggy_function() {         return 'bad result';     }     function other_functions(){         return 'blah';     } } 

What can I do to replace buggy_function()? Obviously this is what I would like to do

class third_party_library redefines third_party_library{     function buggy_function() {         return 'good result';     }     function other_functions(){         return 'blah';     } } 

This is my exact dilemma: I updated a third party library that breaks my code. I don't want to modify the library directly, as future updates could break the code again. I'm looking for a seamless way to replace the class method.

I've found this library that says it can do it, but I'm wary as it's 4 years old.

EDIT:

I should have clarified that I cannot rename the class from third_party_library to magical_third_party_library or anything else because of framework limitations.

For my purposes, would it be possible to just add a function to the class? I think you can do this in C# with something called a "partial class."

like image 578
SeanDowney Avatar asked Sep 26 '08 00:09

SeanDowney


People also ask

What is the difference between redefining and overriding a method?

Overriding: The method in the ancestor class is virtual. A new implementation overrides the virtual one. Redefining: The method in the ancestor class isn't virtual. Therefore the compiler has to choose which method gets used at compiletime.

How do you redefine a method in SAP?

You can redefine a method using one of the following: Add your code to the existing code, remove asterisks from the existing code, save and activate the class. Remove existing code, add your own code, then save and activate your class.

What is the difference between redefining and overriding a method in Java?

Redefining is with Static Methods. Static methods are associated with Class and not with Object, so we do not override as per instance for run-time. In case of static we are just redefining the method.


2 Answers

It's called monkey patching. But, PHP doesn't have native support for it.

Though, as others have also pointed out, the runkit library is available for adding support to the language and is the successor to classkit. And, though it seemed to have been abandoned by its creator (having stated that it wasn't compatible with PHP 5.2 and later), the project does now appear to have a new home and maintainer.

I still can't say I'm a fan of its approach. Making modifications by evaluating strings of code has always seemed to me to be potentially hazardous and difficult to debug.

Still, runkit_method_redefine appears to be what you're looking for, and an example of its use can be found in /tests/runkit_method_redefine.phpt in the repository:

runkit_method_redefine('third_party_library', 'buggy_function', '',     'return \'good result\'' ); 
like image 113
Jonathan Lonowski Avatar answered Oct 06 '22 20:10

Jonathan Lonowski


runkit seems like a good solution but its not enabled by default and parts of it are still experimental. So I hacked together a small class which replaces function definitions in a class file. Example usage:

class Patch {  private $_code;  public function __construct($include_file = null) {     if ( $include_file ) {         $this->includeCode($include_file);     } }  public function setCode($code) {     $this->_code = $code; }  public function includeCode($path) {      $fp = fopen($path,'r');     $contents = fread($fp, filesize($path));     $contents = str_replace('<?php','',$contents);     $contents = str_replace('?>','',$contents);     fclose($fp);              $this->setCode($contents); }  function redefineFunction($new_function) {      preg_match('/function (.+)\(/', $new_function, $aryMatches);     $func_name = trim($aryMatches[1]);      if ( preg_match('/((private|protected|public) function '.$func_name.'[\w\W\n]+?)(private|protected|public)/s', $this->_code, $aryMatches) ) {          $search_code = $aryMatches[1];          $new_code = str_replace($search_code, $new_function."\n\n", $this->_code);          $this->setCode($new_code);          return true;      } else {          return false;      }  }  function getCode() {     return $this->_code; } } 

Then include the class to be modified and redefine its methods:

$objPatch = new Patch('path_to_class_file.php'); $objPatch->redefineFunction("     protected function foo(\$arg1, \$arg2)     {            return \$arg1+\$arg2;     }"); 

Then eval the new code:

eval($objPatch->getCode()); 

A little crude but it works!

like image 37
JPhilly Avatar answered Oct 06 '22 19:10

JPhilly