views:

444

answers:

2

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  A: 

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.

troelskn
+3  A: 

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.
porneL
I believe the Factory method has some limitations which I can't remember right now. I'll try either way.
knuck
Factory solution is limited to one plugin extending one class. Use something like stickleback if you need arbitrary number of plugins.
porneL