Page 1 of 1
Eval: Error Handling Potentially fatal code
Posted: Thu Nov 02, 2006 9:06 am
by bluedoor
Hi all -
I'm working on a CMS and, of course, am using eval to took process the code coming from the database. I'm not too worried about this security wise, as only admins will have access to modifying the evaled code.
Anyways, what I want is that if there is some kind of error in the evaled code, the template will still show up, just that the content might be messed up or not there. Right now, when the content has a fatal error, the entire script halts executing, and thus the template never has a chance to output.
Is it possible to handle the errors in the evaled code? If there's a fatal error in the evaled code, I don't want it to stop everything!
Thanks,
Ryan
Posted: Thu Nov 02, 2006 9:43 am
by Weirdan
I'm working on a CMS and, of course, am using eval to took process the code coming from the database.
"Of course"? It's not that common to use eval at all.
Anyways, what I want is that if there is some kind of error in the evaled code, the template will still show up, just that the content might be messed up or not there. Right now, when the content has a fatal error, the entire script halts executing, and thus the template never has a chance to output.
You can output template via
register_shutdown_function(). It will get processed even if there was a fatal error somewhere in the script.
Posted: Thu Nov 02, 2006 9:45 am
by malcolmboston
Weirdan wrote:
You can output template via
register_shutdown_function(). It will get processed even if there was a fatal error somewhere in the script.
interesting....i never knew it could kep going in a fatal error.............pretty cool!
Posted: Thu Nov 02, 2006 9:50 am
by Weirdan
interesting....i never knew it could kep going in a fatal error.............pretty cool!
try this:
Code: Select all
<?php
function at_request_end() {
var_dump('AT END!');
}
register_shutdown_function('at_request_end');
asd(); // fatal error, call to undefined function
?>
Posted: Thu Nov 02, 2006 11:14 am
by bokehman
Personally I'd rather put a marker in the DB and the code in a function in a file. You could even include arguments in the DB but not the code.
Posted: Thu Nov 02, 2006 1:36 pm
by AKA Panama Jack
You would be better off saving those code snippets for your database into files that will be loaded as needed. Storing code in a database and then using the eval is very, very bad practice as well as very, very SLOW.
A database should be used to store DATA and not program code. You will find including files is many times faster and uses far fewer CPU cycles than evaling code pulled from a database.
Posted: Thu Nov 02, 2006 4:03 pm
by bluedoor
Thanks Weirdan - that looks great, exactly what I was searching for but couldn't find.
With regard to putting code in a database, I was not aware that evaling code from the database was so much slower. I thought that might be the case, but didn't know.
Since I'm storing content in the db, there's inevitably going to be code stored there. However, as bokehman suggested, if I keep the code in my db down basically to 'include' statements, will this avoid the speed problem? This may be more of a database question, but I wanted to keep it where since we've started to discuss it.
Posted: Fri Nov 03, 2006 1:15 am
by bokehman
AKA Panama Jack wrote:You will find including files is many times faster and uses far fewer CPU cycles than evaling code pulled from a database.
Why? If you are already accessing the DB to get your content why would it be slower because that content contains some code. Also why do you think evaling code would be slower than opening a file? It is exactly what happens when a file is included.
Posted: Fri Nov 03, 2006 3:12 am
by Weirdan
Also why do you think evaling code would be slower than opening a file?
Stab in the dark: eval bypasses opcode caches.
Posted: Fri Nov 03, 2006 6:03 am
by bokehman
Weirdan wrote:Also why do you think evaling code would be slower than opening a file?
Stab in the dark: eval bypasses opcode caches.
If you are already pulling the content from the DB there would be no extra overhead if it contained code. Time saved
ZERO. As far as the difference between eval and require eval is far quicker. The following code returned 440 microseconds for require and 29 microseconds for eval. The evaled code was the same as in the required file.
Code: Select all
<?php
microtime(true);
$start = microtime(true);
require('test53.php');
$end = microtime(true);
echo (($end - $start)*1000000).'<br>';
$start = microtime(true);
eval('$array = array(1,2,3,4,5);');
$end = microtime(true);
echo (($end - $start)*1000000);
?>
Posted: Fri Nov 03, 2006 7:20 am
by Weirdan
If you are already pulling the content from the DB there would be no extra overhead if it contained code. Time saved ZERO. As far as the difference between eval and require eval is far quicker.
What those things you mentioned have to do with opcode cache?
Posted: Fri Nov 03, 2006 7:54 am
by bokehman
Weirdan wrote:What those things you mentioned have to do with opcode cache?
Pass! Is that a leading question?