simplemedia Posted January 7, 2009 Share Posted January 7, 2009 Hi all, great board you got here. I have a question that I think is pretty simple, but I haven't figured it out yet. I have an object that has already been instantiated, lets call it $thing. I'd like to add a new method to $thing, how do I do that? One course of action would be to extend the original class. But $thing is already instantiated, plus I don't know what the original class is (and would like to avoid going that direction if possible.) I tried this, which is similar to Javascript syntax, but it didn't work: $thing->new_method = function() { new function goes in here } Any help is much appreciated, thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/139892-how-to-add-a-method-to-an-existing-object/ Share on other sites More sharing options...
rhodesa Posted January 7, 2009 Share Posted January 7, 2009 as far as i know, you can't extend an object after it's been created. i would recommend just using a function, that accepts the object as an argument. Quote Link to comment https://forums.phpfreaks.com/topic/139892-how-to-add-a-method-to-an-existing-object/#findComment-731900 Share on other sites More sharing options...
premiso Posted January 7, 2009 Share Posted January 7, 2009 It has to be defined inside the class before the script is ran. Once it is ran, that is the object and the definition cannot be changed on the fly. Quote Link to comment https://forums.phpfreaks.com/topic/139892-how-to-add-a-method-to-an-existing-object/#findComment-731901 Share on other sites More sharing options...
simplemedia Posted January 7, 2009 Author Share Posted January 7, 2009 bummer. oh well, thanks for the quick reply! Quote Link to comment https://forums.phpfreaks.com/topic/139892-how-to-add-a-method-to-an-existing-object/#findComment-731903 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.