Reputation: 756
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.
Upvotes: 8
Views: 8923
Reputation: 20948
Here's another use-case for changing an object's class on the fly: upgrading a class to a version that provides debugging and logging, so that developers can see extra logs while actually using the core, logical functionality that the users themselves see.
And the procedure is fairly simple. You start with your original class...
class User {
protected $Id;
protected $Name;
protected $Password;
protected $PostsPerPage;
}
And then you want a new class for the same variable $user
, since you have a new plugin. Remember to use extends
to inherit...
class UserWithPrivilege extends User {
[...]
protected $Visible;
}
And since $user
already has data, you'll need to make a constructor for UserWithPrivilege
that takes the old $user
and reimplements it locally. I.E....
class UserWithPrivilege extends User {
public function __construct($user) {
$this->Id = $user->Id; # and for all other old attributes)
}
}
With this, you'll be able to convert an old class, to a newer version of that class, all using the same variable and without doing much in terms of heavy CPU processing.
Upvotes: 0
Reputation: 2004
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?
Upvotes: 1
Reputation: 100210
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:
__get()
and __call()
class Plugin extends BaseImplementation
and have factory instantiate Plugin
instead of BaseImplementation
). Zend Plugin Loader does something like this.$this->plugin->onFoo()
). There's library for this.Upvotes: 7
Reputation: 117615
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.
Upvotes: 3