Re: PHP eval() fatal error

From: Date: Mon, 27 Aug 2007 05:59:40 +0000
Subject: Re: PHP eval() fatal error
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-261352@lists.php.net to get a copy of this message
When re-writing this example to a form of "create_function", the same problem occurs: <?php $code = ' return 12*A+; '; // Clearly incorrect code :-) $returnValue = 0; // Evaluate formula try { $temporaryCalculationFunction = @create_function('', $code); // E_ERROR here if ($temporaryCalculationFunction === FALSE) { $returnValue = '#N/A'; } else { $returnValue = call_user_func_array($temporaryCalculationFunction, array()); } } catch (Exception $ex) { $returnValue = '#N/A'; } ?> Now I would expect that when I feed "create_function" invalid code (syntax), the function returns false. It doesn't! E_ERROR is thrown right away. In my opinion, this should return FALSE, and only throw an E_ERROR when the created function is executed... Before telling me to read the manual: I did over a hundred times, on both eval and create_function: create_function is not documented to throw E_ERROR right away, instead returning FALSE on error... Exactly what I was thinking using the above code. Now let's repeat my question: is there any way to gracefully evaluate specific code, eventually catch an error and respond to that, without using parsekit() or launching another process to get this done? Regards, Maarten PS: Eval/create_function is in place in my code, I don't see any other method to execute (filtered, of course !!!) PHP code that is embedded in a string. Maarten Balliauw wrote:
Here's the thing: I'm trying to do some dynamic code compilation within PHP using eval(). The code I'm trying to compile raises an E_ERROR (Fatal). Here's a simple example: <?php $code = ' $returnValue = 12*A+; '; // Clearly incorrect code :-) $returnValue = 0; eval($code); ?> Now, I'd like to catch the error made by eval: // ... try { eval($code); } catch (Exception $ex) { var_dump($ex); } // ... Problem persists: a fatal error occurs. Using set_error_handler() and set_exception_handler() is not working either... Is there any way to gracefully catch this error? Regards, Maarten


« previous php.general (#261352) next »