Happy Monday all!
A little while back, I introduced an object oriented programming (OOP) structure known as a class. If you did not read that post or do not know what a class is, check that post out here.
So, what else can you do with classes? They’re pretty nice to have, but don’t they feel just a little bit limited? If you have a generic person class, what if you want to create a class specifically for a farmer? Or maybe an athlete? You would have to create two distinct classes, one for person and one for a farmer. The farmer class will have all of the properties of the person class because a farmer is a person, right? Yes, although some of you may think that a farmer is very “different”, a farmer IS a person!
See, a farmer is a person! Anyways…
To just show an example of what that would look like, I’ll be using the person class from a previous post:
class Person
{
public $sex;
public $hand;
public $hstyle;
public function _construct($mf)
{
$sex = $mf;
$hand = "right";
$hstyle = "straight";
}
}
And then here’s the farmer class:
class Farmer
{
public $sex;
public $hand;
public $hstyle;
public $numcows; // NEW - number of cows
public $numtractors; // NEW - number of tractors
public $numland; // NEW - number of acres of land
public function _construct($mf)
{
$sex = $mf;
$hand = "right";
$hstyle = "straight";
$numcows = 0;
$numtractors = 0;
$numland = 0;
}
}
The variables that I added I noted above next to comments.
Anyways, that just seems like a pain doesn’t it? Don’t you remember the OOP concept of “Don’t Repeat Yourself” (DRY)? (If you don’t, check out my last post here). You’re not supposed to do that in OOP! It’s just WRONG!
Well, good news! There is a way to follow DRY while also having classes like this. This is called inheritence. See, there is a parent/child relationship here (in programming terms all!). The person class is the “parent”, and the farmer class is the “child”. Why is that the case? Well, the farmer class has all of the same properties of the person class, but also has its own properties (or variables) to more uniquely identify itself.
Pretty neat, isn’t it? But, how does that look like in code?

Well, in PHP and other languages that utilize OOP (including Java), they utilize the extends keyword. What does that mean? Well, the farmer class will “extend” the person class, which will allow the farmer class to utilize the base properties of the person class while also having its own. How does this look in code? Here is the person class again:
class Person
{
public $sex;
public $hand;
public $hstyle;
public function _construct($mf)
{
$sex = $mf;
$hand = "right";
$hstyle = "straight";
}
}
And below is the new and improved farmer class:
class Farmer extends Person
{
public $numcows; // NEW - number of cows
public $numtractors; // NEW - number of tractors
public $numland; // NEW - number of acres of land
public function _construct($mf)
{
parent::_construct($mf);
$numcows = 0;
$numtractors = 0;
$numland = 0;
}
}
Whoa, what is that parent::_construct() doing? Well, that is calling the constructor of the parent class, which is Person. That is the main rule of using child classes: the parent class must be constructed before the child class can be used. So, to ensure that the parent class does exist, its constructor is called to construct and set its properties.
Classes are a very useful tool when you know what you are doing with them. What classes have you written? Have you used a child class before?
That’s all for now, until next time…keep that tech gear ticking.
~Goliath7470
