Monday, October 9, 2006

Classes in PHP (object oriented programming)

From a post to a user/developer list I'm on. Since I've never seen a great explanation of ANY object oriented programming, I figured I'd give a brief go at it.

Classes are a code structure from the object-oriented-programming side of PHP. A class is beyond a function. The class definition creates an ideal "object" (rather than an actual object, that comes later), and assigns it properties (variables) and methods (functions). Think of "object" in this case as a function on steroids.

Here's the basic class syntax for how I would deal with shoelaces (I'm using comments for a real life physical action that I don't feel like emulating in real code) -- but note that an "object" isn't usually a physical object -- it could be an email, data, graphics, form buttons, or something even more abstract.

class Shoelaces {
var $tied = false;
// maybe we should double-knot them?
var $double = false;

function tie_shoelaces () {
if ($this->tied) { return; }
// instruction to grab shoelaces here
if($this->double) {

function pull_tight() {
// yank whatever is in your hands
// in opposite directions
function overhand_knot() {
// loop item in right hand around item
// in left hand one complete rotation
// hold item in right hand again
function make_loop($side) {
// creates a loop from the left
// or right side of the lace

Here's how you use the class within the program flow (this code would be in the master body of your program -- or in another function
or class):

// create an object instance -- this is a specific local
// copy of the class for this specific instance/call.
$bootlaces = new Shoelaces;
// set the properties
$bootlaces->tied = false;
$bootlaces->double = true;
// tie the shoelaces (call the method i.e. execute the function)

Now you should have a doubleknotted shoelace on your boots. This code can easily be reused for $sneakers also. Or even hacked for tying laces on other objects.

Now, the parts people have the hardest time wrapping their brain around:

  • Calling it "object" "method" "instance" etc. -- It's a prepackaged set of code (class), functions (methods) and one particular time you call the code (instance -- such as $bootlaces or $sneakers). Call them what you want to but remember the standard synonyms so you aren't confused when you read about them.

  • The term "$this" inside the class definition -- $this is "$this object" or a place holder for the namespace INSIDE the object. I guess that once you call an instance, PHP translates $this to $bootlaces or $sneakers -- it's a generic "variable" that points back to the parent object and allows you to keep your scope within the object clear.

  • Translating this idea to actual code since I wimped out for understanding by using a physical not computing activity, so see a practical real-world paradigm for a class below.

There is no place to run code in the object without defining a function/method. It's confusing to call it an object method when obviously you're even using the syntax "function" :P

I didn't use it above, but there's also something called a "constructor method" and in PHP5 there's a destructor method. All this means is "automatically run this function when I create the class instance" or a destructor is something auto-run when you destroy the object instance (not sure how, I don't do PHP5 yet -- maybe unset()?)

Syntax for a constructor in PHP4 is to name the constructor function the same name as the class name -- inside the class definition.

	function Shoelaces() {}

I know this is probably confusing, so feel free to ask questions. Note however that this completely protects the code and variables of the class from colliding with the names of other items in the program. The only name of concern is the class itself. Other people can use your class pretty easily -- it makes a very portable chunk of code. There's more to it, but this is enough to actually get you using this idea if you decide it would make code more efficient.

A real example would be creating an "Email" class where you can set up emails to be sent.

class Email {
var $to = "";
var $from = "";
var $subject = "";
var $body = "";

function Set_Params ($to, $subject, $body, $from=$this->from) {
//assign to class values
$this->to = $to;
//etc. ...

// a couple internal functions not used outside the
function validate_email () {
//validation in here
function make_headers () {
//create headers including From - output to class variable
$this->headers = $headers;

function SendEmail() {
// validate TO & FROM email addresses
// create headers
// send the email (should add custom From header too)
mail($this->to, $this->subject, $this->body, $this->headers);

$email4Larry = new Email;
// set the properties
"", "Hi, Larry", $body);

More some other time if anyone's interested.

No comments:

Post a Comment