11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

CHAPTER 8 • ERROR AND EXCEPTION HANDLINGwww.it-ebooks.info}}/* Add the e-mail address to the database */function subscribeUser() {echo $this->email." added to the database!";}}// Assume that the e-mail address came from a subscription form$_POST['email'] = "someuser@example.com";/* Attempt to validate and add address to database. */if (isset($_POST['email'])) {$subscribe = new Subscribe();if($subscribe->validateEmail($_POST['email']))$subscribe->subscribeUser($_POST['email']);}?>You can see that it’s possible for two different exceptions to fire, one derived from the base class andone extended from the InvalidEmailException class.SPL’s ExceptionsIntroduced in Chapter 7, the Standard <strong>PHP</strong> Library (SPL) extends <strong>PHP</strong> by offering ready-made solutionsto commonplace tasks such as file access, iteration of various sorts, and the implementation of datastructures not natively supported by <strong>PHP</strong> such as stacks, queues, and heaps. Recognizing theimportance of exceptions, the SPL also offers access to 13 predefined exceptions. These extensions canbe classified as either being logic- or runtime-related, with X of the exception classes extendingLogicException and X extending RuntimeException, respectively. All of these classes ultimately extendthe native Exception class, meaning you’ll have access to methods such as getMessage() and getLine().Definitions of each exception follow:• BadFunctionCallException: The BadFunctionCallException class should be used tohandle scenarios where an undefined method is called, or if an incorrect numberof arguments are called in conjunction with a method.• BadMethodCallException: The BadMethodCallException class should be used tohandle scenarios where an undefined method is called, or if an incorrect numberof arguments are called in conjunction with a method.188

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

Saved successfully!

Ooh no, something went wrong!