mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 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 More sharing options...
mikesta707 Posted August 26, 2009 Author Share Posted August 26, 2009 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+"()"); } Link to comment https://forums.phpfreaks.com/topic/171986-dynamically-add-functions-to-a-class/#findComment-906873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.