[PHP] how to trap eval error?

2003-11-18 Thread david
Hi, 
i am new to this list as well as to PHP. i am in a situatin where i want to 
eval a string like:

eval('$return = $function($input);');

where $function is a string specify a function to call and $input is the 
input parameter for the function. $return is just whatever is returned by 
the $function. my problem is that if $function is NOT defined anywhere, i 
got a fatal error like:

Fatal error: Call to undefined function: ... snip

does anyone who how to trap this error if it can be trap at all? what i have 
done so far is something similar to:

set_error_handler('myHandler');
eval('$return = $function($input);');

that doesn't seem to work at all as 'myHandler' is never called. i have 
alose tried to check $php_errormsg but that doesn't seem to be helpful as 
well. as a last resort, i tried:

error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
set_error_handler('myHandler');
eval('$return = $function($input);');

that does seem to make the error stop appearing but my handler is still not 
called. any idea?

thanks!
david

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to trap eval error?

2003-11-18 Thread Jay Blanchard
[snip]
eval('$return = $function($input);');
[/snip]

The problem is the quotes...the string is not truly being eval'd. Change
to double quotes

eval($return = $function($input););

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] how to trap eval error?

2003-11-18 Thread david
Jay Blanchard wrote:

 [snip]
 eval('$return = $function($input);');
 [/snip]
 
 The problem is the quotes...the string is not truly being eval'd. Change
 to double quotes
 
 eval($return = $function($input););

thanks for the tip but i am sure you mean:

eval(\$return = \$function(\$input););

otherwise the variables gets expanded before they get to eval and i end up 
with a syntax error. i found a solution (hopefully) with:

if(function_exists($function)){
eval('$return = $function($input);');
}else{
// function does not exists
}

which works quit nicely for now. not sure if that's a good thing to do.

thanks!
david

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to trap eval error?

2003-11-18 Thread CPT John W. Holmes
From: david [EMAIL PROTECTED]


 i found a solution (hopefully) with:
 
 if(function_exists($function)){
 eval('$return = $function($input);');
 }else{
 // function does not exists
 }
 
 which works quit nicely for now. not sure if that's a good thing to do.

Why not just do this:

if(function_exists($function))
{ $return = $function($input); }
else
{ //function does not exist; }

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to trap eval error?

2003-11-18 Thread david
Cpt John W. Holmes wrote:

 From: david [EMAIL PROTECTED]
 
 
 i found a solution (hopefully) with:
 
 if(function_exists($function)){
 eval('$return = $function($input);');
 }else{
 // function does not exists
 }
 
 which works quit nicely for now. not sure if that's a good thing to do.
 
 Why not just do this:
 
 if(function_exists($function))
 { $return = $function($input); }
 else
 { //function does not exist; }
 

because i didn't know PHP can do that. thanks for the tip! any differences 
between the 2 version in turns of performance and safety?

david

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to trap eval error?

2003-11-18 Thread Eugene Lee
On Tue, Nov 18, 2003 at 02:03:25PM -0800, david wrote:
: 
: Cpt John W. Holmes wrote:
: 
:  From: david [EMAIL PROTECTED]
:  
:  if(function_exists($function)){
:  eval('$return = $function($input);');
:  }else{
:  // function does not exists
:  }
:  
:  which works quit nicely for now. not sure if that's a good thing to do.
:  
:  Why not just do this:
:  
:  if(function_exists($function))
:  { $return = $function($input); }
:  else
:  { //function does not exist; }
: 
: because i didn't know PHP can do that. thanks for the tip! any differences 
: between the 2 version in turns of performance and safety?

You don't have to invoke eval()), and you don't have to worry about
quoting issues in eval().

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] how to trap eval error?

2003-11-18 Thread CPT John W. Holmes
From: david [EMAIL PROTECTED]
 Cpt John W. Holmes wrote:
  From: david [EMAIL PROTECTED]
 
  i found a solution (hopefully) with:
 
  if(function_exists($function)){
  eval('$return = $function($input);');
  }else{
  // function does not exists
  }
 
  which works quit nicely for now. not sure if that's a good thing to do.
 
  Why not just do this:
 
  if(function_exists($function))
  { $return = $function($input); }
  else
  { //function does not exist; }
 

 because i didn't know PHP can do that. thanks for the tip! any differences
 between the 2 version in turns of performance and safety?

You're not invoking eval() for one thing and there's less of a security risk
compared to passing variables into an eval() function.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php