Jump to content


Photo

Loose function definitions?


  • Please log in to reply
4 replies to this topic

#1 dwees

dwees
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts
  • LocationUnited Kingdom

Posted 10 October 2006 - 04:11 PM

Hi there,

I've written some Javascript that uses the new Canvas element to produce images in HTML, which is great, but the images aren't permanent, and I'd like to make them so.  Also, Canvas isn't supported in all browsers, and seems lack text capability and anti-aliasing to create sharp pictures.

So I'd like to start from scratch and recode the whole thing in PHP, and I thought a good idea would be to basically convert as much of the code as I can directly from the Javascript, then work around the details, and use $_POST to send the data from the client side back to the server, then return an image depending on what information the server-side script receives, and even better, using PHP I could cache the images so that if an identical call is made by the user, I can just immediately return the image.  All of this seems like it won't be an issue for me, however...

In the Javascript I have a pair of nested functions defined like so:

function f(x, fu) {	
	var myfunction = new Function("x", "return "+ fu + ";")
	return myfunction (x);
}

where x is a number, and fu is a string.  When I do this, I can construct the required function from the user input, and then use that constructed function to modify the number x.  Basically the myfunction is not strictly defined, and the definition can be adjusted at runtime.

Is this type of definition possibly in PHP?  I took a look at my one skinny PHP book I have, and I didn't see any examples of how to do this, so before I go and buy a more expensive fat PHP book, I thought I'd ask around and see if anyone can help me with this.

Thanks in advance,

Dave

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 10 October 2006 - 04:16 PM

I've done something like this using eval();

http://usphp.com/eval

#3 dwees

dwees
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts
  • LocationUnited Kingdom

Posted 10 October 2006 - 04:17 PM

Thanks for the super fast response, Eval looks like what I want to use. 

Dave

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 October 2006 - 06:24 PM

You should also look at the function create_function().  This may be closer to your original idea.

Ken

#5 dwees

dwees
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts
  • LocationUnited Kingdom

Posted 11 October 2006 - 07:10 AM

This create_function thing has virtually identical syntax to what I was using in Javascript, thanks!

I'm pretty sure I can work it around either way, but option #2 is closer to what I was doing in Javascript.  Actually, if you include  a var $newfunc = etc... it's identical syntax.

Dave




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users