what are classes in php? How are they used. I have seen them, just dont understand how they work.
what is "->" used for?
Printable View
what are classes in php? How are they used. I have seen them, just dont understand how they work.
what is "->" used for?
The class is central to object orientated design methodology. In object orientated design you build a systems by putting together a set of objects. Much like a body contains many organs; each doing there own thing to accomplish a task and made to work together to form an organism.
In object orientated design; an object usually represents a single entity within the system (i.e: an order). The object can execute its own precesses (methods) and communicate with other objects via its state (properties).
A class is simply a template for an object. It describes how it behaves and how it should interact with other objects in a system. You use a class to create an instance of an object.
The above example demonstrates a simple class called Human. Disregarding the simplistic and possibly biased view of humanity. The functions in the class represent methods reproduce() is a defined behaviour. The getXXXXX() methods are accessors/getters and allow you to access properties declared as private (private means only accessible within the object).PHP Code:class Human
{
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
private $dob;
private $gender;
private $children = array ();
private $mother;
private $father;
public function __construct($gender)
{
$dob = Now();
$this->gender = $gender;
}
public function reproduce(Human $partner, $noChildren)
{
if ($partner->getGender() == $this->gender()) {
thrw new Exception('This version of humanity does not support same sex couples.');
}
if ($this->gender == self::GENDER_MALE) {
$mother = $partner;
$father = $this;
} else {
$mother = $this;
$father = $partner;
}
for ($x = 1; $x<=$noChildren; $x++) {
$child = = new Human(rand(GENDER_MALE, GENDER_FEMALE);
$child->setMother($mother);
$child->setFather($father);
$this->children[] )
}
}
public function getGender()
{
return $this-> gender;
}
public function getMother()
{
return $this->mother;
}
public function getFather()
{
return $this->father;
}
public function setMother(Human $mother)
{
if ($mother->getGender() != self::GENDER_FEMALE) {
throw new Exception ('Gender Adnormality');
}
$this->mother = $mother;
}
public function setFather(Human $father)
{
if ($father->getGender() != self::GENDER_MALE) {
throw new Exception ('Gender Adnormality');
}
$this->father = $father;
}
public function getDOB()
{
return $this->dob;
}
public function getChildren()
{
return $this->children;
}
}
-> is the object access operator $o->function() executes a method $o->property returns a public property. The :: operator is the scope resolution operator. This is used to access constant and static properties and methods.
You create an instance of an object using the new operator:
The benefits of object orientated design are clear. Firstly you can encapsulate data and hence protect it inside an object by declaring it private. There are three others too which I let you read up on:PHP Code:$male = new Human(Human::GENDER_MALE);
$female = new Human(Human::GENDER_FEMALE);
$male->reproduce($female, 4);
- Encapsulation.
- Polymorphism.
- Inheritance.
- Abstraction.
OOP is the best thing since sliced bread. I recommend it to any web developer in conjunction with the MVC design pattern.
P.s: Read the PHP5 OOP tutorial in my sig ;)
wow. Ok, thanks va!