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 Quote 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+"()"); } Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.