<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMPlights &#187; exceptions</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using SPL Exceptions</title>
		<link>http://www.hermanradtke.com/blog/using-spl-exceptions/</link>
		<comments>http://www.hermanradtke.com/blog/using-spl-exceptions/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 00:00:56 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=194</guid>
		<description><![CDATA[Brandon Savage has a great series of posts on using exceptions in PHP.  Unfortunately, he does not introduce the SPL exceptions into the discussion. The Standard PHP Library (SPL) has quite a few exception classes that are useful right out of the box.  Additionally, these exceptions can be extended just like the Exception and ErrorException [...]]]></description>
			<content:encoded><![CDATA[<p>Brandon Savage has a <a href="http://www.brandonsavage.net/exceptional-php-nesting-exceptions-in-php/">great</a> <a href="http://www.brandonsavage.net/exceptional-php-extending-the-base-exception-class/">series</a> of <a href="http://www.brandonsavage.net/exceptional-php-introduction-to-exceptions/">posts</a> on using exceptions in PHP.  Unfortunately, he does not introduce the SPL exceptions into the discussion.</p>
<p><span id="more-194"></span></p>
<p>The Standard PHP Library (SPL) has quite a few exception <a href="http://www.php.net/manual/en/spl.exceptions.php">classes</a> that are useful right out of the box.  Additionally, these exceptions can be extended just like the Exception and ErrorException classes.</p>
<p>My favorite is the <a href="http://www.php.net/manual/en/class.invalidargumentexception.php">InvalidArgumentException</a>.  I use this exception if some parameter to a function or method is not what I expected.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> doubleMe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'Unable to double a non-numeric value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you are using the __call() magic method, the BadMethodCallException is another great built-in exception to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> BadMethodCallException<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;Method <span style="color: #006699; font-weight: bold;">$name</span> does not exist&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The SPL exception classes are a great alternative to writing your own exceptions classes.  For some more examples you can check out PHPUnit.  Sebastian Bergmann uses a lot of SPL exceptions in his code there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/using-spl-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

