Tuesday, November 30, 2010

Taking advantage of Exceptions in PHP

Another post about errors.

Exceptions are often overlooked because of the extra setup you need to do in order to create a good design even though I myself am not a guru of any object oriented design principles. Nevertheless, a programmer should take advantage of its capabilities.

 Here is a simple way of using an extended exception in php.

$franz = 'alien';
 try{
   if($franz != 'human'){
     throw new My_Extended_Exception('Error, you are not a human. Go back to your planet', 306);
   }
} catch (Exception $e) {
   echo $e->logError()->outputError();
}

The neat part with extending an exception is you can play with the class and do your own logging. As an example.

class My_Extended_Exception extends Exception
{
  protected $_errorMessage;

  public function __construct($message, $code)
  {
    parent::__construct($message, $code);
  }

  public function logError()
  {
    $this->_errorMessage = $this->message . ' code:' . $this->code . ' on line: ' . $this->getLine() . ' in file: ' . $this->getFile() . PHP_EOL;
     file_put_contents("errors.txt", $this->_errorMessage, FILE_APPEND | LOCK_EX);
    
     return $this;
  }
   
   // sends the error to the browser;
   public function outputError()
  {
    if($this->_errorMessage){
      return $this->_errorMessage;
    }
  }
}

A little about the code. By extending our exception we added a little ability to log our error messages then also output it to the browser. We basically automate the process and should have control on our errors.
Another big plus using an exception is the native methods of the error class. Here I used 2 which is getLine() and getFile(). Pretty self explanatory which will get the line number of the error and also the file where it came from. There are more of these methods in php.net documentation. I also passed the $message and $code argument to the parent class which is Exception in order to access the message and code property directly (eg: $this->message).

And thats it! hope it helped some.

3 comments:

Unknown said...

2015-09-16 zhengjx
Michael Kors Outlet Stores Online
canada goose outlet
Kobe Shoes,Kobe 9,Kobe 8 Discount
Coach Factory Outlet Official Coach Handbags Sale
ray-ban sunglasses
Louis Vuitton Handbags Outlet Online
michael kors bag
Coach Outlet Online Coach Factory Outlet
Louis Vuitton Handbags UK For Sale
Louis Vuitton Handbags Official USA Website
timberland boots
ugg boots
Oakley Sunglasses Official Website Cheap Off
coach factory outlet online
nike trainers
toms outlet
celine handbags
michael kors outlet
Hollister UK Clothing Store
Louis Vuitton Outlet Discount Handbags
Cheap Louis Vuitton Handbags Luggage Bags Sale
cheap jordans
hollister uk
michael kors bags
ugg boots sale
Gucci Outlet Online 80% OFF
ugg australia
michael kors outlet
Mont Blanc Pens For Sale
Michael Kors Outlet Online 70% Off Handbags

Unknown said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

chenlina said...

chenlina20151127
michael kors outlet
canada goose
louis vuitton outlet
north face outlet
cheap jordan shoes
abercrombie kids
michael kors handbags
coach factory outlet
hollister
air max 90
air max uk
michael kors outlet online
cheap oakley sunglasses
michael kors outlet online
polo ralph lauren outlet
michael kors outlet
christian louboutin outlet
michael kors outlet
kids lebron shoes
prada handbags
coach factory outlet
ugg sale
coach outlet
cheap oakley sunglasses
cheap jordans
cheap uggs for sale
cheap uggs
hollister uk
fitflops
ugg boots
michaek kors handbags
tiffany and co
tod's shoes
true religion outlet
michael kors outlet
louis vuitton outlet
ugg sale
jordan 11s
cheap ray ban sunglasses
michael kors outlet
as