PHP runtime class modification

人盡茶涼 提交于 2019-11-27 02:53:50

问题


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.


回答1:


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.



回答2:


PHP doesn't allow this. It may be a dynamic language in other respects, but the class system is deliberately restrictive. You can either install the runkit extension, which changes the language to allow mocking about with classes at runtime (But then you aren't using plain PHP anymore), or you can use the magic-methods to simulate it.




回答3:


You can override the class, but I don't know if you can reload it in the same request. some behavior change can be achieved with mediator design pattern (symfony event dispatcher) but you need to know the extension points in advance, and fire events/messages to be caught by an extending class in the future..

if you can wait for the next request, and clear cache if you have it. I made a tool that might help you. SourceEditor

Here there are more answers to a similar question too, where I put code examples. How to generate or modify a PHP class at runtime?



来源:https://stackoverflow.com/questions/1593497/php-runtime-class-modification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!