<?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>David Calhoun&#039;s Developer Blog &#187; php</title>
	<atom:link href="http://davidbcalhoun.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://davidbcalhoun.com</link>
	<description>Just another blog</description>
	<lastBuildDate>Fri, 03 Feb 2012 01:29:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP: dealing with stuff that doesn&#8217;t exist</title>
		<link>http://davidbcalhoun.com/2009/php-dealing-with-stuff-that-doesnt-exist</link>
		<comments>http://davidbcalhoun.com/2009/php-dealing-with-stuff-that-doesnt-exist#comments</comments>
		<pubDate>Tue, 01 Dec 2009 08:09:13 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[exists]]></category>
		<category><![CDATA[function_exists]]></category>
		<category><![CDATA[isset]]></category>
		<category><![CDATA[method_exists]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=41</guid>
		<description><![CDATA[Introduction As the application you&#8217;re coding becomes more and more complex, there&#8217;s more of an opportunity for things to go wrong in all sorts of unforseen ways.  One of these problems is variables or functions that don&#8217;t exist.  The problem is when you code in such a way that you presuppose their existence. But their [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>As the application you&#8217;re coding becomes more and more complex, there&#8217;s more of an opportunity for things to go wrong in all sorts of unforseen ways.  One of these problems is variables or functions that don&#8217;t exist.  The problem is when you code in such a way that you presuppose their existence.  But their existence isn&#8217;t guaranteed!</p>
<p>A simple case of this happening is when you&#8217;re dealing with user input.  As it turns out, the user didn&#8217;t fill out a text field, but the variable for that text field still gets passed around as if it did exist.  More specifically, the variable gets set, but it gets set to an empty string (&#8221;).</p>
<h3>Check that the variable isn&#8217;t empty</h3>
<p>A common way of dealing with this is to check that the variable isn&#8217;t empty before using it:</p>
<pre name="code" class="PHP">$name = 'Joe';
if($name != '') {
	echo 'Hello ' . $name;
}</pre>
<p>This turns out to be ALMOST equivalent to the following, which uses <a href="http://www.php.net/manual/en/function.empty.php">empty()</a>:</p>
<pre name="code" class="PHP">$name = 'Joe';
if(!empty($name)) {  // make sure $name isn't empty
    echo 'Hello ' . $name;
}
</pre>
<p>However, as <a href="http://www.zachstronaut.com/posts/2009/02/09/careful-with-php-empty.html">Zachary Johnson</a> points out, empty($variable) evaluates to true when $variable=&#8217;0&#8242;, so you may want to avoid using this.</p>
<h3>Check that the variable is defined</h3>
<p>There&#8217;s another case we haven&#8217;t tried yet: what happens if we haven&#8217;t defined the variable?</p>
<pre name="code" class="PHP">error_reporting(E_ALL);
if($name != '') {
	echo 'Hello' . $name;
}</pre>
<p>Nothing catastrophic happens, but if we turn on error reporting, we can see that PHP is a bit upset.  We get a notice that the variable $name is undefined, yet we&#8217;re trying to evaluate it.  The value hasn&#8217;t been set yet.  And there&#8217;s a function to check for that:</p>
<pre name="code" class="PHP">$name = 'Joe';
if(isset($name)) {
	echo 'Variable is set';
}</pre>
<p>We can combine what we&#8217;ve learned into the following:</p>
<pre name="code" class="PHP">$name = 'Joe';
if(isset($name) &#038;&#038; $name != '') {
	echo 'Hello ' . $name;
}</pre>
<p>There&#8217;s one neat thing here &#8211; if $name isn&#8217;t set, it evaluates the first part of the If condition as false (isset($name)) and doesn&#8217;t even look at the second part of the If condition ($name != &#8221;).  This is because it doesn&#8217;t matter what comes after, it will still evaluate to false (false &#038;&#038; true evaluates to false, and false &#038;&#038; false evaluates to false).  Looking at the rest of the If condition is a waste of CPU cycles, so PHP doesn&#8217;t do it!</p>
<p>We can confirm this with the following code, which evaluates to false on the first If condition, then doesn&#8217;t even try to evaluate the call to a nonexisting function, and thus gives no errors:</p>
<pre name="code" class="PHP">if(false &#038;&#038; function_that_doesnt_exist()) {
	// do stuff here
}</pre>
<h3>Check if a function exists</h3>
<p>For functions there&#8217;s a similar helper called <a href="http://us3.php.net/manual/en/function.function-exists.php">function_exists()</a>:</p>
<pre name="code" class="PHP">function my_function() { }
if(function_exists('my_function')) {
	my_function();
}</pre>
<p>And it doesn&#8217;t matter if your function is declared before or after you call it!  Check it out &#8211; this works fine:</p>
<pre name="code" class="PHP">if(function_exists('my_function')) {
	my_function();
}
function my_function() { }</pre>
<h3>Check if an object&#8217;s method exists</h3>
<p>Similarly to checking if functions exist, we can check for the existence of functions belonging to an object (we call these the object&#8217;s methods) with <a href="http://php.net/manual/en/function.method-exists.php">method_exists()</a>:</p>
<pre name="code" class="PHP">// Create a test class
class myClass {
	// Create a test function
	public function my_function() {}
}

// Instantiate the class
$myObject = new myClass();

// Check to see if the test function exists
if(method_exists($myObject, 'my_function')) {
	// Do stuff here
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/php-dealing-with-stuff-that-doesnt-exist/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Tidbit: Dead simple singleton</title>
		<link>http://davidbcalhoun.com/2009/php-tidbit-dead-simple-singleton</link>
		<comments>http://davidbcalhoun.com/2009/php-tidbit-dead-simple-singleton#comments</comments>
		<pubDate>Tue, 15 Sep 2009 05:50:45 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=15</guid>
		<description><![CDATA[class Singleton { private static $instance; public static function getInstance() { if(!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c(); } return self::$instance; } } And the explanation&#8230; class Singleton { private static $instance; // static variable to hold our 1 instance public static function getInstance() { // function to get the 1 instance if(!isset(self::$instance)) [...]]]></description>
			<content:encoded><![CDATA[<pre name="code" class="php">class Singleton {
    private static $instance;
    public static function getInstance() {
        if(!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
    }
}</pre>
<p>And the explanation&#8230;</p>
<pre name="code" class="php">class Singleton {
    private static $instance;               // static variable to hold our 1 instance

    public static function getInstance() {  // function to get the 1 instance
        if(!isset(self::$instance)) {       // this will only run once (and instantiate once)
            $c = __CLASS__;                 // get the class (Singleton)
            self::$instance = new $c();     // instantiate the class and store it in our variable
        }
        return self::$instance;             // return the instance
    }

    public static function myFunction() {   // we can get to this through Singleton::getInstance()->myFunction()
        // ...
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/php-tidbit-dead-simple-singleton/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

