Turbowaffle Posted October 3, 2006 Share Posted October 3, 2006 I want to be able to intercept function calls that are to non-existent functions, and optionally create a function of that name. I want to do something like:foo->getBar();getBar doesn't exist, but I want to intercept the error generated to determine if I want to create the getBar function using the create_function or not. I almost positive I read how to do this in PHP, but I can't seem to find it now. Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/ Share on other sites More sharing options...
Hi I Am Timbo Posted October 3, 2006 Share Posted October 3, 2006 You can use the __call method of php 5 for this. This is a good article about that:[url=http://www.onlamp.com/pub/a/php/2005/06/16/overloading.html]http://www.onlamp.com/pub/a/php/2005/06/16/overloading.html[/url] Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102872 Share on other sites More sharing options...
Turbowaffle Posted October 3, 2006 Author Share Posted October 3, 2006 Bingo! That's exactly what I was looking for. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102874 Share on other sites More sharing options...
Zane Posted October 3, 2006 Share Posted October 3, 2006 why would there be non-existent functions called if you didn't mispell/misname them yourself?I guess I'm just asking for a little more in depth scenario.you say and error is returned and that only happens when the coder makes and error...most of the timeso I'm guessing you're trying to allow open source somethingEDIT:nevermindI got beatVery interesting article BTW...I feel so inspired now Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102875 Share on other sites More sharing options...
Hi I Am Timbo Posted October 3, 2006 Share Posted October 3, 2006 One use for this would be to not have to assign all of the accessor methods for a class. There is __get and __set for that though. Just an example. Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102876 Share on other sites More sharing options...
Daniel0 Posted October 3, 2006 Share Posted October 3, 2006 I don't get the point in it. And Turbowaffle, the link you were shown is PHP5 and upwards code, so make sure your host has that if you're going to use it. Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102878 Share on other sites More sharing options...
Turbowaffle Posted October 3, 2006 Author Share Posted October 3, 2006 Basically, I'm creating a wrapper that calls one of a dozen or so functions available through a CGI via different 'command' strings. So, rather than write a dozen functions, or something like foo->callCgiMethod(xyz), foo->xyz() will work if I add a check in _call to see if it's one of the availble commands and then create it. I could also go through the array and create_function each of them, but I figured it'd use fewer resources to just create the function I need. Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102892 Share on other sites More sharing options...
Zane Posted October 3, 2006 Share Posted October 3, 2006 Yeah, I could only see that idea being used on an as needed basis....seeing as it doesn't append to your class but your object. Quote Link to comment https://forums.phpfreaks.com/topic/22832-creating-functions-on-the-fly/#findComment-102895 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.