PHP运行时类修改 [英] PHP runtime class modification

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

问题描述

所以我希望能够在运行时添加/删除类方法。在你告诉我这是可怕的谚语之前,可能是,但我真的不在乎。我想要这样做的原因是因为我希望应用程序非常模块化,所以一些插件可以扩展一些基类并添加方法,而不会杀死主应用程序。



例如,说我有以下类:

  class User {
protected $ Id ;
protected $ Name;
protected $ Password;
protected $ PostsPerPage;
}

并说,一些插件增加了用户更改其可见性设置的可能性,向类添加$ Visible属性。课程将成为:

  class User {
protected $ Id;
protected $ Name;
protected $ Password;
protected $ PostsPerPage;
protected $ Visible;
}

这可以通过__get和__set实现,但最常见的实现是获取和设置运行时生成的属性,并且为每个添加的属性编写伪getter和setter是一件麻烦,以及使用__set是我的意见中的no-no。



另一个想法是将用户设置分别存储在$ UserVisiblitySettings [userid]之类的另外一个数组中,但这并不像我想要的那样。



下一个想法是做一个帮助课,类似于这样:

  class UserHelper {
public function SetVisiblity($ user_object,value);
}

然后,我可以使用__get和__set来实现朋友的方法/类,但这听起来太骇人听闻。如果我要这样去,也可以重载__call,__get和__set。另外我不太确定这是很好的OOP pratice,它也会看起来很丑陋。



最后一个想法是有一些功能来动态地创建类在运行时使用eval()(这是唯一有效的使用eval我也可以提出)。基本上,从文件中获取类定义,做一些简单的括号查找以查找类的开始和结束括号并发送给eval。



使用runkit是问题,因为它是过时的,我宁愿不强制用户安装一些扩展。



当我看到我的想法,最简单的一个和少cpu密集似乎是要重载_call并添加一个方法来注册在类中。



请不要不要这样做的任何想法

解决方案

RunKit < a>扩展可以做到( runkit_method_add()等)



然而,这是一个实验扩展,已经瞄准了你的脚...



您还有其他选项:




So I want to be able to add/remove class methods at runtime. Before you tell me that's horrible pratice in oop, it might be, but I don't really care. The reason I want to be able to do this is because I want the application to be very modular, so some plugin could extend some base class and add methods to it without killing the the main app.

For example, say I have the following class:

class User {
    protected $Id;
    protected $Name;
    protected $Password;
    protected $PostsPerPage;
 }

And say, some plugin adds the possibility for users to change their visibility settings, adding a $Visible property to the class. The class would become:

class User {
    protected $Id;
    protected $Name;
    protected $Password;
    protected $PostsPerPage;
    protected $Visible;
 }

This could be achieved via __get and __set, but the most common implementation is for getting and setting runtime generated properties, and it'd be a hassle to code pseudo getters and setters for each added property, as well as using __set is a no-no in my opinion.

The other idea I had was to store the user settings separately, in another array like $UserVisiblitySettings[userid], but that makes things not as OO as I would like them to be.

Next idea was to make a helper class, something like this:

class UserHelper {
    public function SetVisiblity($user_object,value);
}

Then, I could use __get and __set to implement "friend" methods/classes, but that sounds too hackish. If I were to go that way might as well just overload __call, __get and __set. Also I'm not so sure this is good OOP pratice, and it'd look ugly too.

The last idea I had was to have some function to create classes dynamically on runtime by using eval() (this is the only valid use of eval I could come up with too). Basically, get the class definition from a file, do some simple bracket finding to find the class' opening and closing brackets and send it to eval.

Using runkit is out of question, as it is outdated and I'd rather not force the user to install some extension.

When I look up to my ideas, the simplest one and less cpu intensive seems to be to overload _call and add a way for methods to be registered in the class.

Please any thoughts that aren't "don't do this" are appreciated.

解决方案

RunKit extension can do it (runkit_method_add(), etc.)

However it's an experimental extension and you're already aiming at your foot...

You have other options:

  • Emulate new fields and methods with __get() and __call()
  • Use subclassing and Factory pattern (class Plugin extends BaseImplementation and have factory instantiate Plugin instead of BaseImplementation). Zend Plugin Loader does something like this.
    It's the solution with least overhead, but also is limited to one plugin extending one class.
  • Add hooks and use delegation (Strategy pattern) ($this->plugin->onFoo()). There's library for this.

这篇关于PHP运行时类修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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