Object Oriented Programming In PHP Part One – Basics 2

Usually, when people visit some website, they quickly form an opinion about it. They either like it, or they don’t. They find it well structured/organized, neat … or give up in search for a contact form … But nobody knows how good is the code, that is running the site … I believe that when programming in PHP, one of the biggest steps towards writing clean and reusable code, is writing object oriented code. That is why I will dedicate a series of blog posts to this topic.

In the first part of this series I will try to lay out the most basic terms as simple as possible. I will describe what is an object, class and a property.

Class

Personally I see a class as some kind of a frame, with which help we generate objects. I would say that the best comparison is the one with a computer game e.g. old strategic game – Age Of Empires. Each building, that we create has a special task. Let’s say it generates soldiers. So, we create a class MilitaryBarack (a building in the game) and that class generates objects (soldiers in the game).

We create a class with a reserved word class ClassName and brackets {}.

class MilitaryBarack 
{
}

Even though this piece of code is perfectly legit, it is pretty useless, because it doesn’t do anything. We will fix that later.

Object

As I mentioned before, class generates, or more accurately, instantiates an object. But what is an object? Let me return back to game comparison. MilitaryBracket generates an object, a soldier, which has certain properties. In programming terms we say that an object is an instance of a class. We instantiate it with the use of a reserved word new plus the ClassName.

$soldier1 = new MilitaryBaracks();
$soldier2 = new MilitaryBaracks();

When we create new instance of an object, it gets a unique identity. We can print object id and all of its properties with very useful function var_dump();.

var_dump($soldier1);
var_dump($soldier2);

If we open that in browser, we will see the output

object(MilitaryBaracks)[1] // note the object id in square brackets. Object has no properties yet.

object(MilitaryBaracks)[2]

Properties

All of this code is still quite useless. To make it more useful, we will add it some properties. Property of an object can contain different types of data. In our case, we could use a soldier’s name and a type of weapon he carries. To assign properties to an object, we have to declare it in its class. PHP offers three types of properties: public, protected and private. I will write more about the meaning of this words in next posts. For now it is enough to know, that they exist. We declare property with one of these reserved words plus a variable.

// class
class MilitaryBaracks {
  // and its properties with defined default values
  public $name = 'Mougli';  
  public $weapon = 'rifle';
}

As you se, we defined two properties to MilitaryBaracks class. If we now instantiate and print a new object, we can see all values it holds. To access the property value we use an arrow (->).

$soldier = new MilitaryBaracks();
echo "Soldier " . $soldier->name . " carries a " . $soldier->weapon;

These is were I stop for today. In part 2 of these series I will write about methods and visibility.

2 thoughts on “Object Oriented Programming In PHP Part One – Basics

  1. Pingback: Object Oriented Programming In PHP Part Two – Methods And Visibility ← Bugs On Steroids

  2. Pingback: Object Oriented Programming In PHP Part Three – Constructor & Destructor ← Bugs On Steroids

Leave a Reply