<?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/"
	>

<channel>
	<title>Kulerwerks</title>
	<atom:link href="http://kulerwerks.collink.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kulerwerks.collink.com</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Wed, 25 Mar 2009 21:19:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Tutorial #2: Object-Oriented PHP4 vs Object-Oriented PHP5 (Intermediate)</title>
		<link>http://kulerwerks.collink.com/php-tutorial-2-object-oriented-php4-vs-object-oriented-php5-intermediate/</link>
		<comments>http://kulerwerks.collink.com/php-tutorial-2-object-oriented-php4-vs-object-oriented-php5-intermediate/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 01:41:24 +0000</pubDate>
		<dc:creator>Collin Klopfenstein</dc:creator>
		
		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[attribute]]></category>

		<category><![CDATA[class]]></category>

		<category><![CDATA[method]]></category>

		<category><![CDATA[object]]></category>

		<category><![CDATA[object-oriented]]></category>

		<category><![CDATA[OOP]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP4]]></category>

		<category><![CDATA[PHP5]]></category>

		<guid isPermaLink="false">http://kulerwerks.com/?p=223</guid>
		<description><![CDATA[Personally, when I write Web applications in PHP, I love to use classes.  They just make things easier, if used properly, in my opinion.  When I first started writing object-oriented PHP (OOPHP), PHP5 was not released yet.  Needless to say, when PHP5 came out, I had to relearn a few things in [...]]]></description>
			<content:encoded><![CDATA[<p>Personally, when I write Web applications in PHP, I love to use classes.  They just make things easier, if used properly, in my opinion.  When I first started writing object-oriented PHP (OOPHP), PHP5 was not released yet.  Needless to say, when PHP5 came out, I had to relearn a few things in order to write proper OOPHP.  In this article, we&#8217;ll be exploring the differences between object-oriented programming (OOP) in PHP4 vs. PHP5.<span id="more-223"></span></p>
<ul>
<li><a href="#introduction">Intro to OOP</a></li>
<ul>
<li><a href="#intro-classes">Classes</a></li>
<li><a href="#intro-objects">Object</a></li>
<li><a href="#intro-attributes">Attributes</a></li>
<li><a href="#intro-methods">Methods</a></li>
</ul>
<li><a href="#oophp4">OOP: The PHP4 Way</a></li>
<li><a href="#oophp5">OOP: The PHP5 Way</a></li>
<li><a href="#inheritance">Inheritance</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
<h3><a name="introduction"></a>Intro to OOP</h3>
<p>While it is a little beyond the scope I had originally intended for this article, I am going to go into a brief explanation of what exactly object-oriented programming is, and why it&#8217;s used.</p>
<p>Object-oriented programming is a way of writing code that is reusable throughout an application.  It&#8217;s also much more than that.  Here are a few of the concepts used in OOP:</p>
<h4><a name="intro-classes"></a>Classes</h4>
<p>Classes are the basis of all object-oriented programming.  They are a &#8220;construct used as a blueprint to create objects&#8221;. (<a href="http://en.wikipedia.org/wiki/Class_%28computer_science%29">Wikipedia: Class (computer science)</a>)</p>
<h4><a name="intro-objects"></a>Objects</h4>
<p>Objects are simply instances of a class.  Usually when an object is instantiated, a method called the constructor is called, if present.</p>
<h4><a name="intro-attributes"></a>Attributes</h4>
<p>Attributes are variables encapsulated in the class.  Generally, without a specific keyword, attributes cannot be accessed without specifying a specific object.  When an object is instantiated, each attribute of the object&#8217;s class is created as variable inside the object.  This might be a little confusing, but we&#8217;ll talk about it more in depth shortly.</p>
<h4><a name="intro-methods"></a>Methods</h4>
<p>Methods are functions encapsulated within a class.  As with attributes, they&#8217;re not accessible without specifying an object unless they&#8217;re declared within the class with a special keyword.</p>
<h3><a name="oophp4"></a>OOP: The PHP4 Way</h3>
<p>I chose to start with the PHP4 way of doing things, because, while deprecated, all of this syntax is still completely valid in PHP5.  There are, however better ways of doing it in PHP5.  If you watch carefully, I&#8217;ll distinguish between what is specific to PHP4, and what is still proper in both.</p>
<p>In PHP, like many languages, you have to use class construct to declare your class.  For example:</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>
<span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// The contents of the class go here</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p>This creates a class called Foo.  Right now there are no attributes or methods in the class.  Let&#8217;s look at how we add attributes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Attributes</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$attr1</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$attr2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$attr3</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$attr4</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attr5</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$att6</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$attr7</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$attr2</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this line will generate a syntax error.  explanation below</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can see, it&#8217;s quite simple and similar to declaring normal variables in PHP.  Please note that the preceding &#8216;var&#8217; is necessary.  In PHP4, this declares a public attribute.  Unfortunately, in PHP4, there is no way to officially declare private and protected attributes, the difference of course being that private and protected attributes can&#8217;t be accessed outside of class methods, but public and protected attributes may be overwritten, while private attributes may not.  We&#8217;ll talk more about inheritance later.  There are, however, naming conventions.  Private attributes are prefaced with an underscore (i.e.: $_myPrivAttr), and protected attributes are prefixed with an underscore and a capital T (i.e.: $_TmyProtAttr).  No extra action is taken on these naming conventions, but an experienced PHP programmer will see these and know what they mean.  I&#8217;ve shown all of the different types of attribute declaration above on lines 4-6.  You can either declare an attribute with no value, as on line 4, or you can give it a constant value, as on lines 5 and 6.  You can also put multiple declarations on one line, as in line 7.  However, you can not try to use a variable or function to set the default value of an attribute.  The above code, due to line 8, will generate the following message:</p>
<pre>Parse error: syntax error, unexpected T_VARIABLE in /Users/collin/class.php on line 8</pre>
<p>Now that we&#8217;ve explored the do&#8217;s and don&#8217;t&#8217;s of attributes, let&#8217;s move on to methods.</p>
<p>As I mentioned before, methods are just functions specific to a class.  In PHP4, they&#8217;re declared exactly the same as a normal function declaration.  For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// attributes</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_first_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_last_name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// methods</span>
  <span style="color: #666666; font-style: italic;">// constructor</span>
  <span style="color: #000000; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_first_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> first_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> last_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> set_first_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$f</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name <span style="color: #339933;">=</span> <span style="color: #000088;">$f</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> set_last_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$l</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collin'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Klopfenstein'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$p</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Let&#8217;s walk through the above example, shall we?  First, you can see we&#8217;ve declared our class (line 2), and our attributes (line 4).  Then we have a method called Person (line 8).  There is a reason this method shares its name with the class.  It is the constructor.  This means PHP automatically calls this function whenever an object of Person is instantiated.  Notice, we&#8217;ve given the method two arguments.  We&#8217;ve chosen to default each argument to null in the event that we don&#8217;t know our person&#8217;s first and last name before instantiation.  In the constructor, we&#8217;re simply calling two other methods in the class to set the first and last name.  Any time that you are accessing class methods or attributes from within a class method, just like outside of the class, you must reference the object.  How do you do this within a class method?  You use <code>$this</code>.</p>
<p>You might be wondering why we made the <code>first_name( )</code> and <code>last_name( )</code> methods.  Well, if you look back up to line 4, you&#8217;ll notice we used the private naming convention for both attributes.  This means that, while in PHP4 PHP will not try to stop us, we should not attempt to access these attributes outside of the class.  Instead we call <code>first_name( )</code> and <code>last_name( )</code>, which return the value stored in their respective attributes.  These aptly-named methods are often referred to as &#8220;getters&#8221;, as they are used to get the value of an attribute.  We also have &#8220;setter&#8221; methods (<code>set_first_name( $f )</code> and <code>set_last_name( $l )</code>).  We use these functions to set the <code>$_first_name</code> and <code>$_last_name</code> attributes of our class.</p>
<p>Now take a look at line 32.  We&#8217;ve talked about instantiation in the last few examples.  Now you get to see what it looks like.  This is how you create an object from a class.  It&#8217;s also perfectly acceptable, if the constructor allows you to not pass any arguments as ours does, to do this instead:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Have you noticed the <code>-></code>?  Wondering what it is?  Whether in a class method (<code>$this-></code>) or outside of the object (in this case <code>$p-></code>), that is how we reference an attribute or method of an object.</p>
<p>The last line of our example code, line 35, is calling a very useful function.  <code>print_r</code> prints our information about a variable (most useful on an array or object) in human-readable form.  Based on our code, this function call will output the following:</p>
<pre>
Person Object
(
    [_first_name] => Collin
    [_last_name] => Klopfenstein
)
</pre>
<p>This kind of information is VERY useful in debugging.</p>
<h3><a name="oophp5"></a>OOPHP: The PHP5 Way</h3>
<p>Now that we&#8217;ve seen the basics of writing object-oriented PHP, let&#8217;s look at the differences in the current version, PHP5.  You&#8217;ll recall that in PHP4, we don&#8217;t have any way to officially declare whether an attribute or method is public, private, or protected.  In PHP5, however, we do have this luxury.  Let&#8217;s begin converting our OOPHP4 to OOPHP5.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// attributes</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_first_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_last_name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// methods</span>
  <span style="color: #666666; font-style: italic;">// constructor</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_first_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> first_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> last_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_first_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$f</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name <span style="color: #339933;">=</span> <span style="color: #000088;">$f</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_last_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$l</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collin'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Klopfenstein'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print</span> <span style="color: #000088;">$p</span><span style="color: #339933;">-&gt;</span>_last_name<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$p</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So far, we&#8217;ve only changed the declaration types.  We want out attributes to be private, so we declared them as such.  To demonstrate what declaring an attribute does, I&#8217;ve tried to print out one of our attributes, <code>$_last_name</code>.  Here&#8217;s what will output:</p>
<pre>Fatal error: Cannot access private property Person::$_last_name in /Users/collin/class.php on line 35</pre>
<p>If we take that line out, you&#8217;ll notice <code>print_r( $p )</code> also has some different output for you:</p>
<pre>
Person Object
(
    [_first_name:private] => Collin
    [_last_name:private] => Klopfenstein
)
</pre>
<p>Notice the <code>:private</code>.  This is PHP&#8217;s way of telling <code>print_r( $p )</code> that <code>$_first_name</code> and <code>$_last_name</code> are private variables.  You&#8217;ve been warned.</p>
<p>Now let&#8217;s look at the difference with constructors/destructors in PHP.  PHP4 does not have destructors by default.  There are ways of accomplishing this in PHP4, but that is beyond the scope of this article.  I suggest you try googling &#8220;PHP4 Destructor&#8221;.  Anyway, now we&#8217;ll take our partially-converted code and change the constructor to a PHP5 constructor, add a destructor, and remove that pesky print accessing a private variable.  Here&#8217;s how it looks now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// attributes</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_first_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_last_name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// methods</span>
  <span style="color: #666666; font-style: italic;">// constructor</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_first_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> first_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> last_name<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_first_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$f</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_first_name <span style="color: #339933;">=</span> <span style="color: #000088;">$f</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_last_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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: #000088;">$l</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_last_name <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collin'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_last_name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Klopfenstein'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$p</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Alright, this is now up to PHP5 snuff.  By no means do you HAVE to have a destructor, or even a constructor, but constructors can be VERY useful.  You can see on line 8 that the constructor has changed from &#8220;Person&#8221; to &#8220;__construct&#8221;.  We&#8217;ve also added a method called &#8220;__destruct&#8221;.  This function is called when the object is destroyed.  Now that we know how to write OOPHP4 and OOPHP5, let&#8217;s move on to a new concept.</p>
<h3><a name="inheritance"></a>Inheritance</h3>
<p>Inheritance is the act of one class inheriting attributes and methods of another class upon instantiation.  For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<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: #000088;">$pub</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$priv</span><span style="color: #339933;">;</span>
  protected <span style="color: #000088;">$prot</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pub</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm public!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">priv</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm private!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prot</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm protected!&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> Foo <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$pub</span><span style="color: #339933;">;</span>
  protected <span style="color: #000088;">$prot</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pub</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar Pub!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prot</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar Prot&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bar<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$bar</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>There should be nothing new here other than <code>extends Foo</code> in the class declaration of Bar on line 13.  This just tells PHP that you want Bar to inherit all of Foo&#8217;s inheritable attributes and methods.  Based on what you&#8217;ve read in this article, can you figure out which attributes Bar will end up with and what their values will be?  Here&#8217;s your answer:</p>
<pre>
Bar Object
(
    [pub] => Bar Pub!
    [prot:protected] => Bar Prot!
    [priv:private] =>
)
</pre>
<p>You&#8217;re probably wondering why it is that <code>$priv</code> is not defined, right?  I mean, it was defined in the constructor of Foo, right?  Right.  The constructor, being a public method, is overridable.  With inheritance, if an attribute or method is declared as public or protected, it&#8217;s overridable.  This means that Bar&#8217;s constructor overrides Foo&#8217;s constructor, rather than inheriting it.  But have no fear.  There is, however, a way to call the parent class constructor.  Here it is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<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: #000088;">$pub</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$priv</span><span style="color: #339933;">;</span>
  protected <span style="color: #000088;">$prot</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pub</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm public!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">priv</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm private!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prot</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm protected!&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> Foo <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$pub</span><span style="color: #339933;">;</span>
  protected <span style="color: #000088;">$prot</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pub</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar Pub!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prot</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bar Prot&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bar<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$bar</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now your output will be this:</p>
<pre>
Bar Object
(
    [pub] => Bar Pub!
    [prot:protected] => Bar Prot
    [priv:private] => I'm private!
)
</pre>
<h3><a name="conclusion"></a>Conclusion</h3>
<p>Alright, so that&#8217;s pretty much it.  If you have any questions, comments, concerns or criticism, just leave a comment.  Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://kulerwerks.collink.com/php-tutorial-2-object-oriented-php4-vs-object-oriented-php5-intermediate/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Tutorial #1: PHP Basics (Beginner)</title>
		<link>http://kulerwerks.collink.com/php-tutorial-1-php-basics-beginner/</link>
		<comments>http://kulerwerks.collink.com/php-tutorial-1-php-basics-beginner/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 00:30:11 +0000</pubDate>
		<dc:creator>Collin Klopfenstein</dc:creator>
		
		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[basic]]></category>

		<category><![CDATA[beginner]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://kulerwerks.com/?p=175</guid>
		<description><![CDATA[Ok, so this begins my series of PHP Tutorials.  I&#8217;m planning on doing some for other languages.  These tutorials will not usually be in any particular order, and usually, unless otherwise specified, they will not be directly related, though some may use things discussed in previous tutorials.  Each tutorial will be ranked [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so this begins my series of PHP Tutorials.  I&#8217;m planning on doing some for other languages.  These tutorials will not usually be in any particular order, and usually, unless otherwise specified, they will not be directly related, though some may use things discussed in previous tutorials.  Each tutorial will be ranked either <em>beginner</em>, <em>intermediate</em>, or <em>advanced</em>.  So, here goes!</p>
<p>In this tutorial, we&#8217;ll be going over the very basics of using PHP in a web page.<br />
<span id="more-175"></span></p>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#basics">Basics</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
<h3><a name="introduction"></a>Introduction</h3>
<p>PHP Is a very easy language to learn, especially if you&#8217;re familiar with a C-like language.  However, it can still prove quite difficult to master.  Don&#8217;t expect to memorize everything you&#8217;ll ever have to do in it.  I&#8217;ve been working in PHP for 5 years now, and I still have to look functions up.  This tutorial assumes you already have a suitable development environment.  There are plenty of tutorials of how to set up Apache/PHP on a wide variety of operating systems, and that is beyond the scope of this article.  Alright, let&#8217;s get our hands dirty.</p>
<h3><a name="basics"></a>Basics</h3>
<p>If you&#8217;re reading this article, you&#8217;re probably familiar with HTML.  If so, you know what .html files, and their outdated, older brothers, .htm files are.  Pages with PHP code in them do not use this file type.  They are suffixed with .php, which just lets our web server know that they need a little bit of pre-processing before they&#8217;re ready to display.  These files, usually, are just HTML with PHP code injected.  This is accomplished by using <code>&lt;?php ?&gt;</code> or <code>&lt;? ?&gt;</code> for shorthand.  While it is perfectly acceptable to use either aforementioned syntax, I generally recommend that you stick with <code>&lt;?php ?&gt;</code> because if you use XHTML, you <em>MAY</em> have issues with collision with <code>&lt;?xml ?&gt;</code> stuff.</p>
<p>Here is an example of how to output &#8220;Hello, World!&#8221; on a page in bold using PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Hello, World Example - PHP&lt;title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;strong&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">print</span> <span style="color: #0000ff;">&quot;Hello, World!&quot;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/strong&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>You can probably deduce exactly what is happening from the code above, but just in case, let&#8217;s break it down.</p>
<ul>
<li><code>&lt;?php</code> - This is lets PHP know it&#8217;s time to do work on 22s.</li>
<li><code>print</code> - This is a construct within PHP. In essence, <code>print</code> is a function, however there are certain rules it does not have to follow that functions do.  For example, we can call it without parenthesis, whereas if I wanted to call a function, I would have to say <code>myFunc( )</code>.</li>
<li><code>"Hello, World!"</code> - This is the string we&#8217;re telling PHP to print out.  This could be anything though: an integer, a floating-point decimal, or an array.  PHP is a very loosely-typed language, meaning that you don&#8217;t have to declare the type of a variable when you declare the variable.</li>
<li><code>?&gt;</code> - This closes the PHP block.</li>
</ul>
<p>Here are some other basic examples of how to do things in PHP:</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>
<span style="color: #666666; font-style: italic;">// This is a comment.  It's skipped by the processor, and is only seen when viewing your code, not on the</span>
presentation side<span style="color: #339933;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># You can also use a pound sign at the beginning of a line to make comments.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
This is a different kind of comment...
...it spans multiple lines.
*/</span>
&nbsp;
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// here we're creating a variable and setting its value to zero</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here are some basic mathematical operations in PHP</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// addition</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// subtraction</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// multiplication</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// division</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here are some more advanced operations</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// modulus. this will divide 3 / 2, and store the remainder in $myVar</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// this will increment the value by 1</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">--;</span> <span style="color: #666666; font-style: italic;">// this will decrement the value by 1</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will add 3 to the current value of $myVar</span>
<span style="color: #000088;">$myVar</span> <span style="color: #339933;">-=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will subtract 5 from the current value of $myVar</span>
&nbsp;
<span style="color: #990000;">print</span> <span style="color: #000088;">$myVar</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this will print 'true', as 0 evaluates to false in PHP</span>
<span style="color: #990000;">print</span> <span style="color: #000088;">$myVer</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this will also print 'true', as 0 also evaluates to null in PHP</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
This is one way of doing an if..else statement.  Notice, the exclamation point in the statement being
evaluated in the if.  That's the equivalent of saying '$myVar == false'.
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is false!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is NOT false!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// There's also &quot;else if&quot;.  For example:</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is null!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$myVar</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is not null or 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// here is another way of doing an if..else.  Notice the colons and the 'endif'.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is false!'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is NOT false!'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// if you only have one line to be executed per condition, then you can do this</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is false!'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is NOT false!'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// You can also mix and match between the first and last I showed you.  For example:</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is false!'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// on the next line we're using the concatenating operator ('.') to outprint the value of $myVar</span>
  <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$myVar</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.  Setting $myVar to null'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$myVar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Finally, there are what are called &quot;ternary&quot; statements.  It's the last type of true if..else</span>
<span style="color: #339933;">!</span><span style="color: #000088;">$myVar</span> ? <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is null!'</span> <span style="color: #339933;">:</span> <span style="color: #990000;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>myVar is <span style="color: #006699; font-weight: bold;">$myVar</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
Here's how this breaks down:
!$myVar ?  print '$myVar is null!' :  print &quot;\$myVar is $myVar&quot;;
|--1--|-2-|----------3-----------|-4-|-----------5-------------|
&nbsp;
1: this is the conditional statement (what you would put in the &quot;if&quot; of the if..else)
2: this operator begins the block of code to execute if the statement is true (the &quot;if&quot;)
3: this is the block of code to execute if the statement is true
4: this operator begins the block of code to execute if the statement is false (the &quot;else&quot;)
5: this is the block of code to execute if the statement is false
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The final type of conditional structure is a switch.</span>
<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$myVar</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is null!'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// note: this automatically jumps out of the control structure</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is 1!'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// we break because we've already found the case we want</span>
  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>  <span style="color: #666666; font-style: italic;">// this is the 'else' of this structure</span>
    <span style="color: #990000;">print</span> <span style="color: #0000ff;">'$myVar is not null or 1!'</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// we don't have to break here, because we're already at the end of the structure</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/*
In some cases you may not want to break.  You may want to match the proper case and then
perform the actions for ever case after that.
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
had we used double-quotes above, the concatenating operator would not have been needed,
though it's usually easier to read.  For example:
/*
print &quot;\$myVar is $myVar&quot;;  // notice we had to escape ('\') the first $ so it shows '$myVar is 0'
&nbsp;
/*
The last thing I'm going to cover in this article is arrays.  There are two ways to use arrays:
  - non-associative
  - associative
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Non-associative Array</span>
<span style="color: #000088;">$myArr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;yellow&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this creates a new array</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
One of the most important array functions you'll use is 'count'.  You simply pass it an array
and it tells you how many values there are in the array.  It's worth noting that count will
always return a number greater by one than the highest index in the array, as the array starts
on 0.  We use this to our advantage ;-)
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// One way to add things to the array is this:</span>
<span style="color: #000088;">$myArr</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myArr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;orange&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// We've added &quot;orange&quot; as the 5th element (index of 4)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Another way</span>
<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$myArr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;purple&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this adds &quot;purple&quot; as the 6th element (index of 5)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Now we move on to associative arrays.  As you may guess by the name, there is an association
made that is not made with non-associative arrays.  Here's how we define an associative array:
*/</span>
<span style="color: #000088;">$myArr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'red'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Red'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'green'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Green'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'blue'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'yellow'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Yellow'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
In this example, we're giving each index a key to access each value by.  The original way using
numbered indexes still works as well;
*/</span>
<span style="color: #990000;">print</span> <span style="color: #000088;">$myArr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this will output 'Red'</span>
<span style="color: #990000;">print</span> <span style="color: #000088;">$myArr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this will output 'Blue'</span>
&nbsp;
<span style="color: #000088;">$myArr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Orange'</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this adds 'Orange' with a key of 'orange' to $myArr</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// You can also remove values from an array.  There are two main ways to do this:</span>
<span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$myArr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this way removes the value at index 0, shifts all indexes down one, and</span>
                     <span style="color: #666666; font-style: italic;">// returns the value removed.</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$myArr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this way just removes the value passed to it and shifts the indexes above</span>
                     <span style="color: #666666; font-style: italic;">// it down one.  It's worth noting that you can unset almost any variable, not</span>
                     <span style="color: #666666; font-style: italic;">// just array indexes</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<h3><a name="conclusion"></a>Conclusion</h3>
<p>Alright, so that&#8217;s it for this tutorial.  If you have any questions, comments, concerns, or criticism, please feel free to leave me a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://kulerwerks.collink.com/php-tutorial-1-php-basics-beginner/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Contact Form &amp; Addition to &#8220;What&#8217;s New&#8221;</title>
		<link>http://kulerwerks.collink.com/contact-form-addition-to-whats-new/</link>
		<comments>http://kulerwerks.collink.com/contact-form-addition-to-whats-new/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 05:00:19 +0000</pubDate>
		<dc:creator>Collin Klopfenstein</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[contact]]></category>

		<category><![CDATA[email]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[kulerwerks]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Twitter]]></category>

		<category><![CDATA[What's New]]></category>

		<guid isPermaLink="false">http://kulerwerks.com/?p=110</guid>
		<description><![CDATA[This weekend was fruitful, in the way of work on Kulerwerks&#8217; website.

I did a bunch of work to get the contact page up.  It&#8217;s only using light validation though.  It&#8217;s checking whether the required values are present.  That&#8217;s all.  I haven&#8217;t implemented any RegEx on the email yet for lack of [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend was fruitful, in the way of work on Kulerwerks&#8217; website.</p>
<p><span id="more-110"></span></p>
<p>I did a bunch of work to get the <a href="/contact">contact page</a> up.  It&#8217;s only using light validation though.  It&#8217;s checking whether the required values are present.  That&#8217;s all.  I haven&#8217;t implemented any RegEx on the email yet for lack of a good RegEx that will actually catch emails with a dot in them (i.e.: john.doe@aol.com).  I can&#8217;t say I&#8217;ve really looked THAT hard either though.  It&#8217;s definitely on my TODO list.</p>
<p>I also got a little bit done to the &#8220;What&#8217;s New&#8221; part of the website on the index.  Now, instead of just having recent tweets from @<a href="http://twitter.com/kulerwerks" target="_blank">kulerwerks</a>, it also has link that switches to the last 3 <a href="/blog">blog posts</a>.  This is also preloaded, so if everything goes according to plan, there&#8217;s no waiting when you click the link for recent posts.</p>
<p>Just adding a little more function to the site&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://kulerwerks.collink.com/contact-form-addition-to-whats-new/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Recent Work to the Site</title>
		<link>http://kulerwerks.collink.com/recent-work-to-the-site/</link>
		<comments>http://kulerwerks.collink.com/recent-work-to-the-site/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 07:47:44 +0000</pubDate>
		<dc:creator>Collin Klopfenstein</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[include]]></category>

		<category><![CDATA[kulerwerks]]></category>

		<category><![CDATA[more]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[site]]></category>

		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://kulerwerks.com/?p=66</guid>
		<description><![CDATA[So I haven&#8217;t quite felt like sleep tonight, and I&#8217;ve been doing some work towards having the site running at full capacity.

Tonight for example, I coded a nifty feature into my &#8216;page&#8217; template.  This feature allows me to include one page&#8217;s content from another page.  For example, on the home page, I&#8217;m using [...]]]></description>
			<content:encoded><![CDATA[<p>So I haven&#8217;t quite felt like sleep tonight, and I&#8217;ve been doing some work towards having the site running at full capacity.</p>
<p><span id="more-66"></span></p>
<p>Tonight for example, I coded a nifty feature into my &#8216;page&#8217; template.  This feature allows me to include one page&#8217;s content from another page.  For example, on the home page, I&#8217;m using the bio page&#8217;s content.  When I get a little more written on the bio page, I&#8217;ll eventually put a &lt;!&#8211;more&#8211;&gt; in there.  The home page will then only show the bio page&#8217;s content up until the &lt;!&#8211;more&#8211;&gt;, at which point, there will be a link to the bio page.  I think this is a pretty useful feature if using WordPress as a CMS, and frankly, I&#8217;m kind of surprised that there&#8217;s not an existing way to do this.</p>
<p>Oh well, divide and conquer, I suppose&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://kulerwerks.collink.com/recent-work-to-the-site/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
