January 12, 2015

Mafia Games Monday (MGM) – What is OOP?

Hello everyone!

So, some of you have been asking how to make your code more compact or not writing the same code over and over again. Well, today, I have some answers for you.

First off, what does OOP stand for? OOP stands for “Object Oriented Programming”. Instead of using countless variables, OOP substitutes these with objects and object references.

How does OOP work? For this part, I would like to use an example to fully explain OOP. First, let’s look at the image below:

Person Class

As you can see, there is a “Person”, who is considered as what’s called in OOP as a class. A class is an association of a group of objects, like in this case, people. Elaya, Dara, and Bharath are what OOP calls instances of the class (or objects). They are specific definitions of the person class with their own specific traits.

So, with that in mind, let’s do an actual PHP example.

First, we will need to create a class. In PHP, a class is defined like this:

class MyClass
{
}

Of course, there is much more to a class than this, but that’s the shell. So, in following the example I gave to explain a class versus an object, let’s create a class. So, we will do the skeleton first:

class Person
{
}

Now that we have that, let’s create some class properties, which I consider to be labeled as different traits of a class. So, with this example, let’s do sex (male/female), preferred hand (left/right), and hair style:

class Person
{
public $sex;
public $hand;
public $hstyle;
}

Wait, what’s with the “public” in front of the variables? Well, that is what’s called the visibility of the property. Basically, public properties can be accessed by anyone, protected properties can be accessed by the class itself, parent classes, and inherited classes (parent and inherited classes will be talked in a future post). I think a great visual of this is shown by the image below:

Pub_Priv_Prot

Wait, why not make everything public then?

Expose All Values

Well, not so fast! You don’t want to do that. I will talk in a future blog post as to why, but for now, just trust me on this one. 🙂

So, after this, we will create what’s called the constructor. The constructor is what it sounds like: it builds the class object when you ask for a class object to be created. This is what it looks like (shown in the example we’re doing):

class Person
{
public $sex;
public $hand;
public $hstyle;

public function _construct($mf)
{
$sex = $mf;
$hand = "right";
$hstyle = "straight";
}
}

So, the _construct function is what is called when you want to create a new class object. $mf is a function parameter that is called, which in this case will be the sex (male or female). In the constructor, I defined the hand and hstyle properties to be a default that I decided.

Ok, so this is all and good, but how do I use this? Well, to create a new class object, outside of the class definition, write this line:

$stanley = new Person("male");

Yep, that’s it. Pretty simple, right? Ok, so how do you define the other two properties for Stanley? Well, since they’re public, it’s pretty simple:

$stanley->hand = "left";
$stanley->hstyle = "curly";

There are other ways to set property values, which are shown here: http://php.net/manual/en/sdo.sample.getset.php.

This is simply just the beginning. I will go into more detail about some other OOP-related topics (including DRY, inherited classes, and parent classes) later on.

That’s all for now, until next time…keep that tech gear ticking.

~Goliath7470