views:

2953

answers:

7

Is there any way to redefine a class or some of it's methods without using typical inheritance ex:

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

what can I do to replace the 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 delima, i did an update to 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 weary 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 due to the framework i'm using

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

+1  A: 

Zend Studio and PDT (eclipse based ide) have some built in refractoring tools. But there are no built in methods to do this.

Also you wouldn't want to have bad code in your system at all. Since it could be called upon by mistake.

Ólafur Waage
+3  A: 

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


Others have pointed out that runkit is available, which is described as the replacement for classkit. My understanding of its description is that it uses sandboxing to enable monkey patching and redefinitions (including constants). Though, to do so, it requires all function and method bodies be defined in strings (not my cup of tea).

However, it's been deemed broken with PHP 5.2+ by its own maintainer(s) and has had less than 20 sporadic Subversion updates since the last release (~3.5 years ago), despite decent activity before then. The project's lead, Sara Golemon, hasn't touched the project since October 2006.


A few patch projects of runkit exist, adding support for PHP 5.2+. So far, I've found these on Github:

Jonathan Lonowski
this was accepted as the answer: does that mean that PHP supports it? some more information would be nice.
nickf
@nickf: "I don't believe" is a negation; so the OP means "not supported in PHP". As far as I can tell, **PHP does not support monkey patching**.
Piskvor
@Piskvor, well yes - I can read, but it was curious that this answer was accepted over the other ones which offered some form of guidance.
nickf
+1  A: 

Yes, it's called extend:

<?php
class sd_third_party_library extends third_party_library
{
    function buggy_function() {
        return 'good result';
    }
    function other_functions(){
        return 'blah';
    }
}

I prefixed with "sd". ;-)

Keep in mind that when you extend a class to override methods, the method's signature has to match the original. So for example if the original said buggy_function($foo, $bar), it has to match the parameters in the class extending it.

PHP is pretty verbose about it.

Till
That would normally work however I cannot change the class name due to how framework i'm using is setup
SeanDowney
Then the answer is, you cannot.
Till
Why am I getting downvoted? =)
Till
A: 

There's alway extending the class with a new, proper, method and calling that class instead of the buggy one.

class my_better_class Extends some_buggy_class {
    function non_buggy_function() {
        return 'good result';
    }}

(Sorry for the crappy formatting)

Eric Lamb
A: 

If the library is explicitly creating the bad class and not using a locater or dependency system you are out of luck. There is no way to override a method on another class unless you subclass. The solution might be to create a patch file that fixes the library, so you can upgrade the library and re-apply the patch to fix that specific method.

MOdMac
A: 

You might be able to do this with runkit. http://php.net/runkit

Toxygene
+3  A: 

For the sake of completeness - monkey patching is available in PHP through runkit. For details, see runkit_function_redefine().

Till
thanks for adding a complete answer to this question!
nickf