Jump to content


Photo

Creating functions on the fly


  • Please log in to reply
7 replies to this topic

#1 Turbowaffle

Turbowaffle
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 05:32 AM

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.

#2 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 October 2006 - 05:41 AM

You can use the __call method of php 5 for this. 

This is a good article about that:
http://www.onlamp.co...verloading.html

#3 Turbowaffle

Turbowaffle
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 05:43 AM

Bingo! That's exactly what I was looking for. Thanks!

#4 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 03 October 2006 - 05:43 AM

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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#5 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 03 October 2006 - 05:47 AM

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.

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 03 October 2006 - 05:49 AM

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.

#7 Turbowaffle

Turbowaffle
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 06:40 AM

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. 

#8 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 03 October 2006 - 06:49 AM

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.

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users