OO PHP Primer (v. 4.x)
Warning, I’m new to this.
Warning, I’m assuming you’re not new to OOP.
I’ve hacked away at some PHP before on a couple of projects and I’m now building a Facebook app for a buddy. That’s all going really well but I didn’t want to spend my time slapping away, procedural-style, at the code, I’ve done that before and generally I prefer OOP, thanks.
Here is the first step, PHP4 style. PHP5 appears to have all sorts of useful access modifiers and the like… PHP4 is more widespread but a little less feature rich. For a variety of reasons, mostly to do with default hosting settings we’re sticking to PHP4.
<?php
class MyClass {
var $myInstanceVariable;
function myClassMethod($foo) {
return "hello world $foo";
}
function myInstanceMethod() {
return "hello caller $this->myInstanceVariable";
}
}
$myObject = new MyClass();
$myObject->myInstanceVariable = "tim";
echo $myObject->myInstanceMethod(); //=> hello caller tim
echo MyClass::myClassMethod("harding"); //=> hello world harding
?>