Object-Oriented Programming (OO or OOP) is the best way to have re-usable, sharable, less-bug-ridden, easily readable, easier to debug, and easier-to-pick-up-later professionally written software.
“Learning classes” is the functional way to describe learning object-oriented programming. One skill begets the other. When in college my professor played a video of a band playing music. The band represented a program, and each musician represented a class.
I hope that’s not how it’s described nowadays for comp-sci peeps because it’s a completely backwards way to learn it. It is backwards because you’re already looking at a finished product. To understand OO and classes, think in terms of small portions. Then grow from there.
To give an example of how to build out a class, let’s build a musician or rocker since we’ve been on a Rock Band kick.
Before continuing, this assumes you’ve written some PHP – including at least making a function or two…
.
Now, what does a rocker have?
- Name
- Gender (probably)
- Instrument
- Talent Level
These will be the variables we put in our Rocker class, here’s the code to support us…
<?php
class Rocker {
// OOP classes are usually capitalized. Good form.
var $name;
var $gender;
var $instrument;
var $talentLevel;
}
?>
…
We need to be able to set and get the above information from our Rocker class, so we add some functions in there (‘setters’ and ‘getters’)…
<?php
class Rocker {
…
// Functions (methods) should start as lowercase and then use uppercase to separate words
// (also known as camelCase)
function setName($inName) {
$this->name=$inName;
}
function setGender($inGender) {
$this->gender=$inGender;
}
function setInstrument($inInstrument) {
$this->instrument=$inInstrument;
}
function setTalentLevel($inLevel) {
$this->talentLevel=$inLevel;
}
function getName() {
return $this->name;
}
function getGender() {
return $this->gender;
}
function getInstrument() {
return $this->instrument;
}
function getTalentLevel() {
return $this->talentLevel;
}
}
?>
Now we have a Rocker for which we can set and retrieve attributes. The Rocker class still doesn’t do anything. We can very easily add some functions to the class which do stuff…
<?php
class Rocker {
…
function play() {
// you write this code…
}
function sing() {
// you write this code…
}
function perform() {
this->play();
this->sing();
}
}
?>
You now have a basic Rocker class. You could use it as an object within a Band class or you could use it on its own. A quick example to create my Rocker and make me play…
<?php
$sol = Rocker();
$sol->setName(‘Sol’);
$sol->setGender(‘Male’);
$sol->setInstrument(‘Drums’);
$sol->setTalentLevel(‘31334’);
$sol->perform();
?>
…
This may seem confusing, or even useless, since it really has nothing to do with web development. So let’s switch the term “Rocker” to “PageTemplate”, and switch up the functions so they’re actually useful…
<?php
class PageTemplate {
function getIPAddress() {
return $_SERVER[‘REMOTE_ADDR’];
}
function makeTop($title) {
print ‘HTML that makes up the top of a web page goes here, allowing for a custom $title’;
}
function makeBottom() {
print ‘HTML that closes the page’;
}
}
?>
With the above class, you could easily make a What’s My IP page with just a few lines of code:
<?php
require ‘pageTemplate.php’;
$page = PageTemplate();
$page->makeTop(‘sample title’);
printf(‘our IP address is: %s’, $page->getIPAddress());
$page->makeBottom();
?>
I recommend checking out this entry on devarticles.com: Object Oriented Programming in PHP.
Any questions, don’t hesitate to ask… randomblogaddress31@box2161.temp.domains or @sol.
Love the example. I think students will definately identify. I always had the “clothes in my closet vs. clothes in my son's closet” example for classes. Pshaw. This hits closer to home.
Love the example. I think students will definately identify. I always had the “clothes in my closet vs. clothes in my son's closet” example for classes. Pshaw. This hits closer to home.