Thursday, 8 January 2009
PHP Object Orientation - a brief introduction
This week, I attended PHPNW, which is a monthly meeting for PHP developers in Manchester. While I was there, I got talking to Adrian Hardy about Object Orientated PHP - something which has confused me for a while.
Let me explain. Originally, PHP had no Object Orientation to it at all, but in late version 4, the developers started to add Object Orientated methods and calls to PHP. I never really saw the point of coding in an OO way, but we had a bit of a talk about how OO works, and I must admit, I'm a bit of a convert.
So, for a first example, Adrian talked me through writing some code. Here's what we wrote (ish - I have my own coding standards, so have changed a bit from what he told me to write, but the fundamentals are there):
That's it for now, I hope I've not made it too complicated :)
Let me explain. Originally, PHP had no Object Orientation to it at all, but in late version 4, the developers started to add Object Orientated methods and calls to PHP. I never really saw the point of coding in an OO way, but we had a bit of a talk about how OO works, and I must admit, I'm a bit of a convert.
So, for a first example, Adrian talked me through writing some code. Here's what we wrote (ish - I have my own coding standards, so have changed a bit from what he told me to write, but the fundamentals are there):
class Person {
private $name='';
private $sex=FALSE;
public function setName($name='') {
$this->name=$name;
}
public function setSex($sex='') {
$this->sex=$sex;
}
public function doWalk() {
if($this->name!='') {
if($this->sex!=FALSE) {
echo "Hello, I'm {$this->name}, a {$this->sex}, and I'm going for a walk\r\n";
} else {
echo "Hello, I'm {$this->name} and I'm going for a walk\r\n";
}
}
}
}
$body_one=new Person;
$body_one->setName("Sam");
$body_two=new Person;
$body_two->setName("Bob");
$body_two->setSex("Male");
$body_one->doWalk();
$body_two->doWalk();This should give us the text:Hello, I'm Sam and I'm going for a walkOf course, all this is very good, but there are three reasons I love the idea of OO, and the first is that you can extend a class, and the second is you can auto-populate your class with all it's data using a construct function. So, here's an example of extending the class "Person" (which, in effect copies all the functions and variables from the Person Class to the Man class), and using a construct function. This will return the same result as before, but with two extra lines at the end.
Hello, I'm Bob, a Male, and I'm going for a walk
class Person {
private $name='';
private $sex=FALSE;
public function setName($name='') {
$this->name=$name;
}
protected function setSex($sex='') {
$this->sex=$sex;
}
if($this->sex!=FALSE) {Now looks like:
echo "Hello, I'm {$this->name}, a {$this->sex}, and I'm going for a walk\r\n";
} else {
echo "Hello, I'm {$this->name} and I'm going for a walk\r\n";
}
}
}
}
Class Man extends Person {
public function __construct($name='') {
if($name!='') {$this->name=$name;}
$this->sex="Male";
}
protected function setSex($sex='') {
if($sex!=$this->sex) {echo "Congratulations, you have changed your sex!\r\n";}
$this->sex=$sex;
}
}
$body_one=new Person;
$body_one->setName("Sam");
$body_two=new Man("Bob");
$body_one->doWalk();
$body_two->doWalk();
$body_two->setSex("Female");
$body_two->doWalk();
Hello, I'm Sam and I'm going for a walkThe last thing I like the idea of, is to auto load classes... so, in your index page, or default include, you'd include this code:
Hello, I'm Bob, a Male, and I'm going for a walk
Congratulations, you have changed your sex!
Hello, I'm Bob, a Female, and I'm going for a walk
spl_autoload_register('__autoload_me');
ini_set('include_path', '.');
function __autoload_me($strClass) {
echo "Searching for: $strClass\r\n";
$strFilename="./$strClass.php";
if(file_exists($strFilename)) {
require_once($strFilename);
} else {
return false;
}
}This spl_autoload_register can be included as many times as you want, and is called for each new class that hasn't already been retrieved. You don't need to call it __autoload_me, you could call it Class_Loading_DoHicky or Call_Classes_With_Me or whatever your coding standards say, but doing something obvious like __autoload_me means you'll never miss it :)That's it for now, I hope I've not made it too complicated :)
Labels: Coding Samples, Free Software, Object Orientated, Open Source Software, PHP, projects
Subscribe to Posts [Atom]