25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

132 ” Object Oriented Programming in <strong>PHP</strong><br />

}<br />

echo $e->getMessage();<br />

set_exception_handler("handleUncaughtException");<br />

throw new Exception("You caught me!");<br />

echo "This is never displayed";<br />

i<br />

Note that, because the catch-all exception handler is only called after the exception<br />

has bubbled up through the entire script, it, just like an all-encompassing try...<br />

catch block, is the end of the line for your code—in other words, the exception has<br />

already caused a fatal error, and you are just given the opportunity to handle it, but<br />

not to recover from it. For example, the code above will never output You caught me!,<br />

because the exception thrown will bubble up and cause handleUncaughtException()<br />

to be executed; the script will then terminate.<br />

If you wish to restore the previously used exception handler, be it the default of a fatal<br />

error or another user defined callback, you can use restore_exception_handler().<br />

Lazy Loading<br />

Prior to <strong>PHP</strong> 5, instantiating an undefined class, or using one of its methods in a<br />

static way would cause a fatal error. This meant that you needed to include all of the<br />

class files that you might need, rather than loading them as they were needed—just<br />

so that you wouldn’t forget one—or come up with complicated file inclusion mechanisms<br />

to reduce the needless processing of external files.<br />

To solve this problem, <strong>PHP</strong> 5 features an “autoload” facility that makes it possible<br />

to implement “lazy loading”, or loading of classes on-demand. When referencing<br />

a non-existent class, be it as a type hint, static call, or attempt at instantiating an<br />

object, <strong>PHP</strong> will try to call the __autoload() global function so that the script may be<br />

given an opportunity to load it. If, after the call to autoload(), the class is still not<br />

defined, the interpreter gives up and throws a fatal error.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!