<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
    <channel>
        <!-- This XML Feed shows details for the page Object Oriented PHP -->
        <creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.5/</creativeCommons:license>
        <title>Object Oriented PHP</title>
        <link>http://swik.net/PHP%2FObject+Oriented+PHP</link>
        <description>&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; introduced a revamped Object Model that brings &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; in line with &lt;a class=&quot;wikilink&quot; href=&quot;http://swik.net/oop&quot;&gt;object oriented programming&lt;/a&gt;.&lt;/p&gt;


	&lt;h2&gt;Basics&lt;/h2&gt;


	&lt;p&gt;Classes are defined simply by &lt;strong&gt;&amp;#8220;Class &lt;em&gt;classname&lt;/em&gt; { }&amp;#8221;&lt;/strong&gt;&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
Class HelloWorld
{
  public $word;

  public function print()
  {
    echo $this-&amp;gt;word;
  }
}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Classes contain member functions and variables. the &lt;strong&gt;$this&lt;/strong&gt; pseudo variable is used to reference these variables and functions.&lt;/p&gt;


	&lt;p&gt;In the above example, an object of class HelloWorld can be instantiated and the public function can be called from outside of the object. Where arrays use the syntax $array[&amp;#8216;key&amp;#8217;], Classes use the syntax $object-&amp;gt;var and $object-&amp;gt;method().&lt;/p&gt;


	&lt;p&gt;In this example&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
[...]

$foo = new HelloWorld();  //instantiate the object

$foo-&amp;gt;word = &#039;hello world&#039;;

$foo-&amp;gt;print();
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;This would output &amp;#8220;hello world&amp;#8221;.&lt;/strong&gt;&lt;/p&gt;


	&lt;h3&gt;__construct()&lt;/h3&gt;


__construct is a special method that is used in the first instantiation of an object that allows you to construct the object when it is invoked, as it is triggered whenever a new object is created.

&lt;pre&gt;
&lt;code&gt;
Class HelloWorld
{
  public $word;

  public function __construct($word)
  {
    $this-&amp;gt;word = $word;
  }

  public function print()
  {
    echo $this-&amp;gt;word;
  }
}

$hello = new HelloWorld(&#039;hello cruel world&#039;);

$hello-&amp;gt;print();

&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;This will output &amp;#8220;hello cruel world&amp;#8221;&lt;/strong&gt;&lt;/p&gt;


	&lt;h2&gt;Class Inheritance&lt;/h2&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; classes can extend other classes through the &lt;strong&gt;extend&lt;/strong&gt; keyword. Through this, an extended class inherits methods and members of its parent class defined in the extend declaration.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class HelloWorld
{
  public function print()
  {
    echo $this-&amp;gt;word;
  }
}

class HelloWorld2 extends HelloWorld
{
  protected $word = &#039;hello world&#039;;
}

$hello = new HelloWorld2();
$hello-&amp;gt;print();
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;This will output &lt;em&gt;hello world&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;h2&gt;Visibility&lt;/h2&gt;


	&lt;p&gt;To protect from accessibility pollution, &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; v5 introduces 3 prefixes for declaring class methods or variables: public, protected, and private.&lt;/p&gt;


	&lt;p&gt;Public methods and variables are accessible outside of the class. Protected are only accessible from inside of the class and inherited or parent classes. Private are only accessible from within the class itself.&lt;/p&gt;
</description>
                <category>OOP</category>
        <category>PHP</category>
        <category>php5</category>

        <pubDate>Fri, 23 Jun 2006 22:18:56 -0700</pubDate>
        <lastBuildDate>Mon, 09 Jun 2008 05:57:12 -0700</lastBuildDate>
            
    </channel>
</rss>
