Extending classes at runtime
Extending classes at runtime
am 06.09.2007 01:52:33 von ptdorf
Hi everybody,
one of the most powerful things about ruby is its ability to add
funcionality to a class (or an instance of a class) at runtime. Can
anyone please tell me if this can be done with php?
thanks a million
Re: Extending classes at runtime
am 06.09.2007 10:56:38 von Dikkie Dik
ptdorf@gmail.com wrote:
> Hi everybody,
>
> one of the most powerful things about ruby is its ability to add
> funcionality to a class (or an instance of a class) at runtime. Can
> anyone please tell me if this can be done with php?
>
> thanks a million
>
You could search the manual for "magic methods" and follow the link
"overloading". It may not be what you would do in Ruby, but it should
get you the same possibilities.
Hope this helps,
Dikkie
Re: Extending classes at runtime
am 06.09.2007 10:59:06 von Dikkie Dik
Dikkie Dik wrote:
> ptdorf@gmail.com wrote:
>> Hi everybody,
>>
>> one of the most powerful things about ruby is its ability to add
>> funcionality to a class (or an instance of a class) at runtime. Can
>> anyone please tell me if this can be done with php?
>>
>> thanks a million
>>
> You could search the manual for "magic methods" and follow the link
> "overloading". It may not be what you would do in Ruby, but it should
> get you the same possibilities.
Sorry to follow up on my own post, but you can combine these techniques
with "callback functions". You might want to take a look on that topic also.
Re: Extending classes at runtime
am 06.09.2007 14:12:09 von colin.mckinnon
On 6 Sep, 09:59, Dikkie Dik wrote:
> Dikkie Dik wrote:
> > ptd...@gmail.com wrote:
> >> Hi everybody,
>
> >> one of the most powerful things about ruby is its ability to add
> >> funcionality to a class (or an instance of a class) at runtime. Can
> >> anyone please tell me if this can be done with php?
>
> >> thanks a million
>
> > You could search the manual for "magic methods" and follow the link
> > "overloading". It may not be what you would do in Ruby, but it should
> > get you the same possibilities.
>
> Sorry to follow up on my own post, but you can combine these techniques
> with "callback functions". You might want to take a look on that topic also.
You could create a string with a new sub-class and eval() it ...
$classname='revised';
$newdef=' class ' . $classname . ' extends baseClass {
function extension() { print "Hello world\n"; }
}'
eval($newdef);
$obj=new $classname();
(not tested)
but I find it difficult to see any value in doing this at runtime -
and it opens up a attack vector.
C.
Re: Extending classes at runtime
am 06.09.2007 15:14:54 von ptdorf
the idea is that in a mvc-framework-like application, new modules
(controllers) interact (by adding or redefining) the behavior
(methods, actions) of existing ones. each part of the system should be
able to reach and mess each other (if needed).
for instance, an audit module. you dont need it in every site but in
some its essential. so when you install that module, it should be able
to attach himself to some modules (you may choose which ones in the
admin settings) and from them on every time some method in some
controller is called, the audit makes sure that that action, user,
date and data is tracked.
I found an implementation of this first in drupal with the
hook_nodeapi. that callback allowed new modules to add new fields at
runtime to certain types of nodes. but the implementation in ruby was
much cleaner. the same for js (with the prototype) and, if im not
wrong, java aop (aspect-oriented programming) achieves the same.
thanks Dikkie for pointing the __call method together with a callback
though i need to think better on this.
Re: Extending classes at runtime
am 06.09.2007 15:45:05 von zeldorblat
On Sep 6, 9:14 am, ptd...@gmail.com wrote:
> the idea is that in a mvc-framework-like application, new modules
> (controllers) interact (by adding or redefining) the behavior
> (methods, actions) of existing ones. each part of the system should be
> able to reach and mess each other (if needed).
>
> for instance, an audit module. you dont need it in every site but in
> some its essential. so when you install that module, it should be able
> to attach himself to some modules (you may choose which ones in the
> admin settings) and from them on every time some method in some
> controller is called, the audit makes sure that that action, user,
> date and data is tracked.
>
> I found an implementation of this first in drupal with the
> hook_nodeapi. that callback allowed new modules to add new fields at
> runtime to certain types of nodes. but the implementation in ruby was
> much cleaner. the same for js (with the prototype) and, if im not
> wrong, java aop (aspect-oriented programming) achieves the same.
>
> thanks Dikkie for pointing the __call method together with a callback
> though i need to think better on this.
Look at this:
Re: Extending classes at runtime
am 06.09.2007 16:04:09 von ptdorf
thanks, that the whole point: add, rename, redefine, remove methods at
runtime.
but somehow im not too fond of pecl. i'd rather see those kind of
features implemented in pure php code.
Re: Extending classes at runtime
am 06.09.2007 16:09:07 von luiheidsgoeroe
On Thu, 06 Sep 2007 15:14:54 +0200, wrote:
> the idea is that in a mvc-framework-like application, new modules
> (controllers) interact (by adding or redefining) the behavior
> (methods, actions) of existing ones. each part of the system should be
> able to reach and mess each other (if needed).
>
> for instance, an audit module. you dont need it in every site but in
> some its essential. so when you install that module, it should be able
> to attach himself to some modules (you may choose which ones in the
> admin settings) and from them on every time some method in some
> controller is called, the audit makes sure that that action, user,
> date and data is tracked.
Sounds more like an Observer pattern is needed? No screwing with classes,
just enforce an 'observable' interface unto classes in need of observing,
and add the right Observers to it?
--
Rik Wasmus
Re: Extending classes at runtime
am 06.09.2007 16:40:08 von ptdorf
yes but its way more than just wait for certain event to happen and
respond to it. maybe the audit module was not the best example (the
observer pattern fits perfect for the audit).
a more complex example: some sites dont need to manage user roles and
permissions. one admin takes care of all. but in others, this is
essential. when editing the objects, they need to set the roles you
want to grant some kind of access. in these case, the html and table
fields must be created on demand and then when running the list/show
action, the code to check your access it must decide to display to
this object or not must be called.
the tricky part is that all should be self contained. i dont want the
other modules (pages, documents) to be aware or to bother with this
stuff. i want them to be as lean as possible. their only concern is
manage and display their own basic data. so all the responsability
rest upon the roles module. its up to it to manage all this alone and
mess with the editing/display process. and if it isnt installed,
perfect. the site runs faster.
drupal amazing architecture allow this kind of behavior in a
procedural enviroment. but im using oop and mvc and i'd prefer a
cleaner code.