OOP in PHP Part One - Classes and Inheritance
Before we start, this tutorial is meant for those who are at an intermediate level of PHP.
OOP stands for Object-Oriented Programming. It is a programming concept that caught on in the ***0's. OOP focuses on 'objects' which are, well, objects. They have certain characteristics, and can behave in certain ways. OOP programming has a few concepts that define it. One of the defining features we will start with is called a class.
A class shows what an object has and can do, and it consists of members. Members can be divided into properties and methods. Properties are the characteristics of the object. For example, cheese (object) has the properties of type (maybe Gorgonzola, or cheddar), color (green, or white), and flavor (awful or delicious). Methods are the actions and behaviors the object can do. For example, cheese (object) can mold. Now let's see the ********* side of it.
[PHP]class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these * properties
// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}
function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}[/PHP]
You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.
Now let's see this class in action.
[PHP]<?php
class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these * properties
// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}
function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}
$zargento = new Cheese; // Zargento is a brand of cheese
// We will now give it characteristics
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");
// Now let's see those details
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
?>[/PHP]
You make a new object by using 'new'. $zargento is now a Cheese object. It has access to all the properties and methods we outlined in the class. If we want $zargento to use the giveDetails() function, you use '$zargento->giveDetails()' as was used above. If you run the above script, the output will be:
Code:
Gorgonzola
Awful
Green and white
Now that you've gotten a good idea of how classes work, we can move one step further with another concept of OOP called inheritance. This allows you to create new classes using already created classes. Here is an example:
[PHP]class MoreCheese extends Cheese {
var $cost;
function giveCost($f) {
$this->cost = $f;
}
function showCost() {
return $this->cost;
}
}[/PHP]
You use 'extends' to grab the methods and properties from the Cheese class and add them to the MoreCheese class. Let's see the full code.
[PHP]<?php
class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these * properties
// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}
function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}
class MoreCheese extends Cheese {
var $cost;
function giveCost($f) {
$this->cost = $f;
}
function showCost() {
return $this->cost;
}
}
$zargento = new MoreCheese;
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");
$zargento->giveCost("2*.**");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();
?>[/PHP]
As you can see, even though $zargento is no longer Cheese, and is now MoreCheese, it still retains all of the methods and properties from Cheese because the MoreCheese class inherits all of them from Cheese. The advantages to inheritance are that you don't have to edit the base class (in this case Cheese). You can also override code, as this example shows.
[PHP]<?php
class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these * properties
// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}
function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}
class MoreCheese extends Cheese {
var $cost;
function giveDetails($q) {
echo $q;
}
function giveCost($f) {
$this->cost = $f;
}
function showCost() {
return $this->cost;
}
}
$zargento = new MoreCheese;
$zargento->giveDetails("dilly");
$zargento->giveCost("2*.**");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();
?>[/PHP]
As you can see, the giveDetails() function of the base class Cheese has been overrided by the giveDetails() function of the MoreCheese class. This is a very useful feature of inheritance.
Thank you for reading this part of the tutorial, and I hope you learned something
~Moonbat
"Workers of the world unite; you have nothing to lose but your chains." -Karl Marx