View Full Version : Php...oop


hdshngout
11-06-2006, 02:12 AM
I've been wondering this for some time and I haven't been able to find a good answer.

What is Object Oriented Programming (in PHP) and why would I want to use it?

So far, I've gotten


[20:22] Douglas: It does great stuff....
[20:23] Douglas: phpbb3 is totally OOP
[20:23] Douglas: It organises stuff so you can remember where it is... its hard to explain

Anyone got any other explanations?

bejayel
11-06-2006, 05:54 PM
class Person
{
private $firstName;
private $lastName;

function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}

function getFirstName()
{
return $this->firstName;
}

function getFirstName()
{
return $this->lastName
}
}

class Employee extends Person
{
private $pay;

function __construct($firstName, $lastName, $pay)
{
parent::__construct($firstName, $lastName);
$this->setPay($pay);
}

function setPay($pay)
{
if($pay >= 0)
{
$this->pay = $pay;
}
}

function getPay()
{
return $this->pay;
}
}


In this example that i just wrote (so it may or may not work), you can see how an Employee is actually a part of a person. By extending person i have essentially made an Employee a Person, but i can still use those common variables to make, say an Employer. Now all of my common methods have been groups tegether :).

Also objects are serializable, so they can be stored within sessions.

Object also take away the boundries of the default data types within PHP.

If i want to finda person, i can now make an equals method in the person object, and viola, no longer restriected :). At the low level, i will still be using php's comparing, but at the high level, this makes it a lot easier to locate information for common stuff.