Teaching class classes for PHP development – Rock Band Example

PHP classes

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;
}
?>


Continue Reading