General – Exceptions

The issue: PHP has exceptions now, cool but dangerous.
  • Exceptions are a great tool for handling exceptional states
  • Exceptions are often misused and become regulars
  • Exceptions may make PHP leaking
<?php
function foo() {
if ($user_input !== "Hello!") {
throw new Exception("User input wrong");
}
}
?>
Only use exceptions, when your application ends up in an exceptional state
<?php
function bar() {
if (connection_timedout()) {
throw new Exception("Connection timeout");
}
}
?>
Exceptions leak a little bit of memory when thrown e.g. out of for loops
<?php
foreach ($i = 1000000; $i > 0; $i--) {
throw new Exceptions("Hehe, I'm leaking");
}
?>
Allocated memory from the for-loop-header is not freeed correctly.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s