What are objects and why would I ever use them?

 This was a big question on my mind two years ago when PHP 5 just started getting popular.  Now today I can truly say there isn't a piece of code I write that doesn't look a using objects. But let's not get ahead of ourselves.  PHP added OOP, thats a fancy acronym for Object Orientated Programming.  I've struggled with this term for years and when it was first introduced to me in C++ it didn't make any sense.  What I failed to see was how my base parent class could build a foundation that my program would thrive on.

 Alright so I've had a tough time thinking of a good analogy to what OOP really is and the bicycle spoke diagram never worked because some where they always lost me on how it related to my code.  So instead of trying to gloss over this topic lets take a good hard look at it.  Here is a start class called foo and a base structure:

class foo{
public $myval1;
}

Like I said real simple. You can see to define a class it is like defining a function but instead we use the key word class here.  Also you can see how I have defined a class variable.  The word public allows us to access this variable from outside the class.  If we wanted only to use this variable inside the class we could of used the private designation instead.  But for now lets just enjoy a public variable.

So how would we use this simple class?  Where there are a couple of ways.  You can define a new variable that initializes the class like so:

$myClass = new foo();

Or if you only need one instance of this class you could always access its variables and function like:

foo::myval1 = 10;

If you're following along so far you're probably thinking this is great another way to contain my function and variable but where is the OOP in this?   For a few months I used classes to help protect my data but they were just a fancier way to hold my code instead of making more files.  But today we will see how this all works.  But with this we need to learn a new key word "extends".  This is where the power of any OOP program comes into play.  Let look at a second class now that uses this:

class foo2 extends foo {
public $myval2;

When we now use foo2 we have access to all the function and variables in foo plus the new variable $myval2.  So you may ask yourself how does this help.  Well let's expand on our first class and add some functions that take care of setting and retrieving our values so here is what function foo should look like:

class foo {
public $myval;

function set( $key, $val ){
$this->$key = $val;
return true;
}

function get( $key ){
return $this->$key;
}

}

The two new functions we added don't seem like much but say you build foo into you main class for handeling you database access.  These function could check and validate your values before you allow them to be stored or retrieved.  Its a lot easier to do it at this level than to write these kind of functions for every class or situation.  Now when you call foo2 you have access to these two new great functions wihtout having to rewrite foo2's definition.

This gives us flexability that normaly would take a lot of work to add into your code without classes.  In the next article we will explore how to build our foundation class that we will build into our foundation classes.