Extending classes at runtime

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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

ptdorf@gmail.com wrote:
Quoted text here. Click to load it
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,

Re: Extending classes at runtime

Dikkie Dik wrote:
Quoted text here. Click to load it

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

Quoted text here. Click to load it

You could create a string with a new sub-class and eval() it ...

$newdef=' class ' . $classname . ' extends baseClass {
  function extension() { print "Hello world\n"; }
$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.


Re: Extending classes at runtime

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

On Sep 6, 9:14 am, ptd...@gmail.com wrote:
Quoted text here. Click to load it

Look at this:


Re: Extending classes at runtime

thanks, that the whole point: add, rename, redefine, remove methods at

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

Quoted text here. Click to load it

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

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.

Site Timeline