重定义类方法或类 [英] Redefine Class Methods or Class

查看:143
本文介绍了重定义类方法或类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法重新定义一个类或其一些方法而不使用典型的继承?例如:

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

我可以做什么替换 buggy_function()?显然这是我想做的

  class third_party_library重新定义third_party_library {
function buggy_function(){
返回'好结果';
}
function other_functions(){
return'blah';
}
}

这是我的两难选择:破坏我的代码的库。我不想直接修改库,因为将来的更新可能会再次破坏代码。我正在寻找一种无缝的方式来替换类方法。



我找到此图书馆



编辑:



我应该澄清,我不能将类从 third_party_library 重命名为 magical_third_party_library 或任何其他因为框架限制。 / p>

为了我的目的,是否可以只向类添加一个函数?我认为你可以在C#中使用称为部分类的东西。

解决方案

它被称为猴子修补。但是,PHP没有原生支持它。



但是,正如其他人也指出, runkit库可用于添加对语言的支持,并且是 classkit 。而且,虽然它的创建者似乎已经抛弃了兼容PHP 5.2和更高版本),该项目现在似乎有一个新的家庭和维护者。 / p>

我还是不能说我是粉丝其方法。通过评估代码字符串进行修改对我来说总是潜在的危险和难以调试。



仍然, runkit_method_redefine 以及其使用示例,请访问 /tests/runkit_method_redefine.phpt

  runkit_method_redefine('third_party_library','buggy_function','',
'return \'good result \''
);


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."

解决方案

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\''
);

这篇关于重定义类方法或类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆