Jump to content

Dynamically add functions to a class


mikesta707

Recommended Posts

Is there a way to dynamically add a function to an already existing class? What I want to do is create a function that will allow the user to define new functions that are not already in the class. I have the following so far which doesn't produce any errors, but doesn't work even in the slightest

 

function setCustomCriteria(func, name, where){
eval(func);
eval("this."+name+"="+name);
eval("this."+name+"()");
}

 

and here is where I call it

 

function go(){ alert("Worked!"); }

object.setCustomCriteria(go, "go", "nowhere");

 

I'm sure that is horribly wrong, but that was just a shot in the dark because, frankly, im sort of lost

Link to comment
https://forums.phpfreaks.com/topic/171986-dynamically-add-functions-to-a-class/
Share on other sites

Nevermind, figured it out. This is awesome!

 

function setCustomFunc(name){
       //The line below will add the function that whatever class this function is part of
eval("this."+name+"="+name);
        // the line below will execute the function
//eval("this."+name+"()");


}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.