Overly Simple: PHP 5 Object Orientation
If you're a newbie/intermediate programmer like me, you're likely beginning to see the light concerning object oriented programming (OOP). Encapsulating functions and data within objects makes code more maintainable and flexible. My first introduction to OOP was with Visual Basic .NET and (Unfortunately) Visual Studio 2005. Why is this so unfortunate? Well, my introduction to OOP took place within the confines of a fancy-pants editor with that ever-so-insidious temptress: code completion (Perhaps I'll articulate my thoughts on that in the future). What I'm getting at is that I gained only topical understanding of how OOP worked without ever learning the reasons and benefits of implementing it. Once I started using OOP techniques in PHP 5, I began to understand why OOP is useful:
- Maintainability: Once code is organized into classes that are analogous to logical objects (For example: "account", "user", "page", "tool", etc), it is easier to analyze, revise, and update.
- Reusability: If classes are designed well, they can be reused in many other aplpications. For example, imagine you have a class that manages connecting to a MySQL database. Chances are that that code would be useful in many other applications. Once you have a class, it is easy to move around and use in different applications.
- Protection: As in: you code is protected from you (Read: Your logical, intelligent you is protected from your bumbling, idiot you). Once your code is in a class, you can declare variables and functions as private, meaning that they can only be accessed from within the class itself. Using this protection, you will achieve data encapsulation, which basically just means that data within your object (See below) can only be altered through functions that are in the class. This will make more sense after further reading...
There are tons of other reasons why OOP is a good idea, but this is just a basic introduction. Now I'll provide an overly simple explanation of the concept of OOP design using PHP as an example:
Earlier I mentioned the term class as well as object. The bare-bones concept of OOP is that you put a bunch of related variables and functions in a class, and then implement the class by instantiating an object from the class. Once you have a new object instantiated, you can access all the public functions and variables of the class through that instance.
In PHP 5, classes are declared just like functions except you use the keyword class instead of function, and you don't include any parathesis after the class name:
private $hairLength = 5;
public $lastWordsHeard;
function analogWeapon() {
echo "Hello, I'm analog.weapon";
}
public function cutHair($howMuch) {
if ($howMuch > $this->hairLength) {
$this->$hairLength = 0;
} else {
$this->hairLength -= $howMuch;
}
}
public function getHairLength() {
return $this->hairLength;
}
}
So now we have a class (analogWeapon) with a private variable ($hairLength), a public variable ($lastWordsHeard), and 3 functions. Notice that the first function has the same name as the class: This function is called the constructor. This function will run automatically every time a new object is instantiated from this class. So every time we make a new analogWeapon, he says, "Hello, my name is analog.weapon".
Another thing to note is the use of the keywords private and public before variable declarations. analogWeapon's hairlength can only be altered though the cutHair() function, which accepts an argument of how much hair should be cut off. It is impossible to change his hairLength in any other way. It is, however, possible to determine the length of his hair. You can ask him by calling his getHairLength() function. That function accesses the private variable (Which it can do because it is inside the analogWeapon class) by using the keyword $this->. Functions and variables of a class can be accessed from within in the class with the $this-> keyword. Note that we use "$this->hairLength" and not "$this->$hairLength".
The last thing to point out before showing how to actually create an object from this class, is the public variable $lastWordsHeard. This variable holds the last few words that were spoken to analogWeapon, and since anyone can talk to analogWeapon, anyone is allowed to change this variable (It is public).
Using the class (Instantiating an object from it), is very easy. In the same file that the class is in, we use the keyword new:
Right off the bat, this will output, "Hello, my name is analog.weapon", because the new keyword instantiates the class which fires the constructor function. The rest is pretty self explanatory:
echo $aw->lastWordsHeard; // outputs nothing (variable is empty)
$aw->lastWordsHeard = "You're neat";
echo $aw->lastWordsHeard; // outputs "You're neat"
$aw->cutHair(3);
echo $aw->getHairLength(); // outputs "2"
echo $aw->hairLength; // error: can't access this variable because it's private
Once you start making a lot of programs OOP style, you'll start building up a library of useful code that you can reuse in other programs. That means you'll be able to spend more time learning new things instead of rewriting code. Here's some stuff to read:
- Object Oriented Programming @ Wikipedia
- A simple overview of PHP 5 OOP usage
- PHP 5: Public, Private, Protected @ Karl Bunyan's blahg
