๐Ÿ“– Object Oriented Programming (OOP)

4 Characteristics of OOP

There are 4 characteristics of OOP that should be understood to make most use of the design pattern.

  1. Abstraction
  2. Inheritance
  3. Encapsulation
  4. Polymorphism

Each of these characteristics may be intuitive based on their names though some may be to abstract to infer. Let's briefly discuss an example of each.

Abstraction

Abstraction is the ability to hide the working parts of the object and only present the interface to the program (or programmer). The example often used is to consider the function of a TV remote control. Does one need to know the inner workings of the device to be able to use it to change the channel? That's abstraction. The 'how' part is abstracted.

In coding, it is mostly about reuse. DRY (don't repeat yourself) coding practices look for duplication in code where an action is repeated. Finding parts of your code that repeats is a good place to identify opportunities to abstract that functionality into a separate class. Then create objects that inherit that function.

Inheritance

Inheritance relies on the principle of abstraction to work. The object of a class inherits the properties and methods of the class. This means that any class property or method will be available to the object created from the class. If the channel needs changing, click the channel button to call the method to change the channel. You don't need to know how it changed the channel, only that the changeChannel() method will change the channel.

Encapsulation

Encapsulation is a method of compartmentalizing the code. We need each object to manage its own state and respond to the program without interference from other programmatic influences. Keeping this responsibility separated from other objects is the job of encapsulation.

Polymorphism

Polymorphism is the ability to modify the attributes of a defined class to meet specific needs. This is often done through extending classes into sub-classes. The parent class is often an abstract class that defines high level functions while the calling sub-class extends that functionality into more specific actions. Your site might have a high level abstract class that processes requests and responses but these are implemented in different ways according to the specific request made.

Usage

Try not to get too hung up in understanding exactly what each of these mean to a beginner programmer. Understanding their place and purpose in programs can take time and experience. You will be guided through the use of these elements before you will be expected to identify each one.

Syntax

One of the biggest differences between a proceduralย  or OOP approach to programming is the syntax of the code. Most procedural code is fairly easy to understand as it proceeds in a linear direction with most of the functionality present. With the built-in abstraction layer of OOP, it can be difficult to understand exactly how a program is progressing and the structural elements don't always lend themselves to this understanding. Below is an example of a simple class that could be used in a PHP application that stores user information and outputs a greeting when called.

<?php

// create a class named User
class User {
  // create a couple of properties
  public $name = "Stephen";
  public $favColor = "blue";

  // create a method named greeting
  public function greeting() {

    return "Greetings " . $this->name . ". Is your favorite color " . $this->favColor . " today?";

  }
}

// create a new (object) instance of the User class
$greeting1 = new User;

// call the greeting() method of the new $greeting1 object
echo $greeting1->greeting();

The above code should output this greeting when the page is loaded. "Greetings Stephen. Is your favorite color blue today?"

The Object Operator

This is just a simple example of how a class is created and used. You should take note of the arrow (->) in the code. It is called the Object Operator and has a special meaning. It is used to access the properties and methods of an object. Notice that it is used in the class definition and also when calling the class elements.

The this Keyword

There is also a special syntactic element that you might not have seen. It is the this keyword. In the context of a class, the this keyword refers to the current object. I say object because it does nothing until an object is created from the class, then it refers to the object. In this case, it would be like saying $greeting1(object)->name(property). Since the class code is written with no prior knowledge of the possible object name, we need a special keyword that can refer to the object once it is created (instantiated).