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

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

问题描述

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

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.

例如,我有以下类:

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

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

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;
 }

这可以通过__get和__set来实现,但最常见的实现是获取和设置运行时生成的属性,这将是一个麻烦的代码伪getter和setters为每个添加的属性,以及使用__set是一个no-no在我看来。

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.

我的另一个想法是将用户设置单独存储在另一个数组,如$ UserVisiblitySettings [userid],但这使得事情不像我想要的OO。

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);
}


$ b < ,但是听起来太黑了。如果我去那样的方式可能只是重载__call,__get和__set。

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.

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

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.

使用runkit是问题,因为它已经过时,我不想强​​迫用户安装一些扩展。

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

当我查找我的想法,最简单的一个和更少的CPU密集似乎是重载_call并为方法在类中添加一个方法。

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 扩展可以做到这一点( runkit_method_add()等)

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

您还有其他选项:


  • 使用 __ get() __ call()
  • 模拟新字段和方法
  • 使用子类化和工厂模式(类插件扩展BaseImplementation ,并且工厂实例化 Plugin ,而不是 BaseImplementation )。 Zend插件加载器做这样的事情。

    这是解决方案最少的开销,但也只限于一个插件扩展一个类。

  • 添加钩子和使用委托策略模式)( $ this-> plugin-> onFoo())。 这里有图书馆

  • 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天全站免登陆