dynamicaly append method to class - workaround for aggregate_methods()
| From: | Karel Kozlik | Date: | Thu, 19 Mar 2009 16:07:43 +0000 |
| Subject: | dynamicaly append method to class - workaround for aggregate_methods() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-290287@lists.php.net to get a copy of this message | ||
Hello list!
I am using in my application dynamic method appending to class with aggregate_methods() function. But this function is no more aviable in php5 and runkit extension seems not to be maintained any more. So I would like to replace it with something more common (__call method or so).
Just to describe the situation (simplified). There is one common class containing some common functions:
class DataLayer{
function add_method($method){
aggregate_methods($this, "DataLayer_".$method);
}
function common_funct_foo(){
...
}
function common_funct_bar(){
...
}
}
And there is hundreds of data manipulation methods (defined within separate classes) which could call common functions. E.g.:
class DataLayer_get_items{
function get_items(){
$this->common_funct_foo();
return something;
}
}
And they could also call other dynamicaly added methods:
class DataLayer_update_attr{
function update_attr(){
$this->get_items();
$this->common_funct_bar();
return;
}
}
All the stuff is used e.g. in this way:
$data = new DataLayer();
$data->add_method('get_items');
$data->add_method('update_attr');
$data->update_attr();
Now the question is whether is it possible to somehow replace functionality of add_method() without aggregate_methods() or runkit_class_adopt(). And _WITHOUT_ need to change the hundreds of DataLayer_* classes. The change should be only in the main DataLayer class.
I was thinking about __call() method, but I do not know how to deal with the $this in dynamicaly appended functions. I need somehow make to $this in these functions reference to instance of DataLayer class.
Any ideas?
many thanks,
Karel