13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

User-Defined Exceptions<br />

197<br />

Listing 7.2<br />

Continued<br />

}<br />

final function getFile() {<br />

return $this->file;<br />

}<br />

final function getTrace() {<br />

return $this->trace;<br />

}<br />

final function getTraceAsString() {<br />

return self::TraceFormat($this);<br />

}<br />

function _toString() {<br />

return $this->string;<br />

}<br />

static private function StringFormat(Exception $exception) {<br />

// ... a function not available in <strong>PHP</strong> scripts<br />

// that returns all relevant information as a string<br />

}<br />

static private function TraceFormat(Exception $exception) {<br />

// ... a function not available in <strong>PHP</strong> scripts<br />

// that returns the backtrace as a string<br />

}<br />

}<br />

? ><br />

The main reason we are looking at this class definition is to note that most of the public<br />

methods are final:That means you cannot override them.You can create your own subclass<br />

Exceptions, but you cannot change the behavior of the basic methods. Note that<br />

you can override the __toString() function, so you can change the way the exception<br />

is displayed. You can also add your own methods.<br />

An example of a user-defined Exception class is shown in Listing 7.3.<br />

Listing 7.3 user_defined_exception.php—An Example of a User-Defined<br />

Exception Class<br />

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

Saved successfully!

Ooh no, something went wrong!