Jump to content

Archived

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

Turbowaffle

Creating functions on the fly

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 time

so I'm guessing you're trying to allow open source something


EDIT:
nevermind
I got beat

Very interesting article BTW...I feel so inspired now

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.