Select Page

The Four Pillars of Object Oriented Programming

OOP is a programming paradigm that computer programmers must encounter in their life. This article assumes you are already familiar with Class & Objects. To enhance your OOP understanding four pillars are worth discussing. So, let’s jump into the details:

Inheritance

The very first pillar is inheritance & it’s totally connected with the real world. As we know, if a father has property then his son will inherit it. And yes every child is going to inherit it. In programming, inheritance complies with the same concept. Let’s see in the code:

	class Father {

		public $name = "Sr. John Doe";

		public function totalProperty() {
			return "USD 1000000";
		}
	}

	class Son extends Father {
		public $name = "John Doe";
	}

	$son = new Son();
	echo "{$son->name} inherit {$son->totalProperty()}"; 

Output: John Doe inherit USD 1000000

Here, we can see the Son class extends the Father class & a $son object. Though the Son class doesn’t contains the totalProperty() method still we can call it by using the son object, this is because of inheritance that has been done using the keyword extends! So if a class extends to another class then we call this class a child class & the main class (Father) is called a parent class. In a simple sense, this is inheritance. The child class will inherit all the properties & methods from the parent class except private data.

Polymorphism

At its base, polymorphism refers to many forms, but that’s not very helpful unless you understand it in the context of OOP. The real value of polymorphism is that objects with the same interface can be called to do different things. In a large complex structure (a great big program), additions and changes can blow the hinges off your program unless it has a common interface (from a parent class or interface). For example, the following program has two classes that share a common interface. The implementations of the interface are quite different: 

	interface ISpeed
	{
		public function speed();
	}

	class Jet implements ISpeed 
	{
		public function speed(){
			return 2500;
		}
	}

	class Car
	{
		public function speed() {
			return 100 + 50;
		}
	}

	$jet = new Jet();
	echo "Jet speed is {$jet->speed()} <br>";

	$car = new Car();
	echo "Car speed is {$car->speed()}";

Output: Jet speed is 2500
Car speed is 150

The two implementations of the ISpeed interface by the Jet and Car classes are very different. However, when making changes or additions within a given structure of a program, I can request or use the interface methods without having to worry about whether my program will crash or not.

One Name with Many Implementations

The easiest way to understand polymorphism is in the different implementations of an interface. The Jet class simply implements each method to return a primitive value, and the Car class makes an addition and then returns a value. Both used the same interface.

Encapsulation

In the previous two examples of inheritance & polymorphism, we have created classes & keep properties and methods inside them. This is basically called encapsulation! Yeah, as simple as it sounds. The term encapsulation simply makes things compact and hides unnecessary details. By using encapsulation, we can set visibility to expose information as per need & making staff secure. If we didn’t wrap properties & methods then it could be accessible from anywhere & it would be unorganized & unmanageable.

Class Abstraction

Abstraction in the real world

I’m a coffee addict. So, when I wake up in the morning, I go into my kitchen, switch on the coffee machine and make coffee. Sounds familiar?

Making coffee with a coffee machine is a good example of abstraction.

You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch them on, and select the kind of coffee you want to get.

The thing you don’t need to know is how the coffee machine is working internally to brew a fresh cup of delicious coffee. You don’t need to know the ideal temperature of the water or the amount of ground coffee you need to use.

Someone else worried about that and created a coffee machine that now acts as an abstraction and hides all these details. You just interact with a simple interface that doesn’t require any knowledge about the internal implementation.

Abstraction in OOP

Objects in an OOP language provide an abstraction that hides the internal implementation details. Similar to the coffee machine in your kitchen, you just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation. But you don’t need to understand how this method is implemented and which kinds of actions it has to perform to create the expected result.

	abstract class CoffeeMachine {

		// Child class must have to follow method signature
		public abstract function getBeans(): string;

		// Child class must have to follow method signature
		public abstract function getMilk(): string;

		// Making Coffee
		public function makeCoffee() {
			$beans = $this->getBeans();
			$milk  = $this->getMilk();
			return 'Hot Cappucino!!!';
		}
	}

	class CoffeeApp extends CoffeeMachine {

		private $beans;
		private $milk;

		public function __construct($beans, $milk) {
			$this->beans = $beans;
			$this->milk = $milk;
		}

		public function getBeans(): string {
			return $this->beans;
		}

		public function getMilk(): string {
			return $this->milk;
		}
	}

	// Client don't need to know coffee is getting ready
	// They just need to pass beans & milk
	$coffeeApp = new CoffeeApp('beans', 'milk');
	echo $coffeeApp->makeCoffee();

Output: Hot Cappucino!!!

Yeah, as a coffee lover, we do care about the output 😀 not what is happening inside the machine!

Suppress PHPCS Warnings Using Comments

PHPCS (code sniffer) is a great tool for linting & checking your codes against the set of rules. Along with PHPCS, as WordPress folk, we use WordPress coding standards (WPCS) as well. It is very useful to maintain coding standards, ensure output escaping, nonce verification, SQL escape, etc. But sometimes, there will be cases to avoid rules checking for specific code snippets.

Let’s discuss the useful comments to suppress warnings

To Ignore code snippets we can use the below comments:

// @codingStandardsIgnoreStart

// @codingStandardsIgnoreEnd

All the codes inside the above comments will be ignored

Ignore just the below line:

//phpcs:ignore

Ignore nonce verification warning:

//phpcs:disable WordPress.Security.NonceVerification.Recommended

Suppress PHPCS(WordPress.DB.PreparedSQL.InterpolatedNotPrepared)

//phpcs:ignore WordPress.DB.PreparedSQL

Ignore nonce verification missing warning

// phpcs:disable WordPress.Security.NonceVerification.Missing

Suppress PHPCS(Squiz.Commenting.FileComment.Missing)

//phpcs:ignore Generic.Commenting.DocComment.MissingShort

PHPCS(WordPress.Files.FileName.NotHyphenatedLowercase)

//phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase

There may have a lot more comments to suppress warnings. If you know others’ comments, feel free to share through the comments section below! Happy coding 😀

Abstract Factory Design Pattern with Example in PHP

Abstract factory method is useful for creating products of related family. Create objects of product from sub-classes. A family could have multiple member and each member could have variation among them but they behave similarly.

For example: Let’s consider a furniture family consist of member like chair, table, sofa. Here member may have different type in looks. Suppose chair may have different types like: modern, victorian, art deco. Similarly table and sofa may have different type as well.

4 Steps for implementing Abstract Factory Design Pattern

I have divided the implementation into four easy steps:

Step 1: Create Factory Abstract Class

Factory class will produce multiple product through abstract method.

abstract class FurnitureFactory
{
    abstract public static function createChair(): ChairInterface;

    abstract public static function createTable(): TableInterface;

    abstract public static function createSofa(): SofaInterface;
}

Step 2: Create Factory Concrete Class

Factory concrete class will extends the Factory class so that it implement abstract method. Factory concrete class will implement each distinct product variation and responsible for creating object of product.


class ModernFurniture extends FurnitureFactory
{
    public static function createChair(): ChairInterface
    {
        return new ModernChair();
    }
    public static function createTable(): TableInterface
    {
        return new ModernTable();
    }
    public static function createSofa(): SofaInterface
    {
        return new ModernSofa();
    }
}

Step 3: Interface of each Distinct Product

Interface will be implemented by the product class so that each product have consistency in their behaviour. This way we don’t have to worry about the variations. Since all variations will implement same interface we can also use property and method of class same way.

interface ChairInterface
{
    public function legs(): int;

    public function sitting();
}

interface TableInterface
{
    public function legs(): int;

    public function hasDrawer(): bool;
}

interface SofaInterface
{
    public function seats(): int;
    public function hasDivan(): bool;
}

Step 4: Declare Product Concrete Class for each Product

Each product will have separate class by implementing interface.

class ModernChair implements ChairInterface
{
    public function legs(): int
    {
        return 4;
    }

    public function sitting()
    {
        return 'fine';
    }
}

class ModernTable implements TableInterface
{
    public function legs(): int
    {
        return 4;
    }

    public function hasDrawer(): bool
    {
        return true;
    }
}

class ModernSofa implements SofaInterface
{
    public function seats(): int
    {
        return 5;
    }

    public function hasDivan(): bool
    {
        return false;
    }
}

Now our product is ready to use by the client code

$modern_chair = ModernFurniture::createChair();
echo $modern_chair->legs();

From this Modern Furniture class will can create table & sofa as well and can show users as relevant product.

In future we need more variations for the furniture we can simple create another variation Factory Concrete class without touching any other code 🙂

Factory Method Design Pattern with Example in PHP

Factory Method is one of the creational design pattern. In simple words, we can say: factory method is a creational design pattern that is mainly focused on object creation in an abstract way from sub-classes. It means we will let the sub-class to create object of product concrete class.

When to use Factory Method design pattern

When we don’t know how many types of product or sub classes could be needed.

Make things loosely coupled

When we need to create object from sub-classes instead of product concrete class

Advantage of Factory Method design pattern

The Factory method design pattern makes the code loosely coupled & reuse able. This way we can follow the single responsibility and open, close principle of SOLID. Besides, we don’t need to worry about the enhancement or scalability of services.

Real Life Example in PHP

Let’s take an example of book library, suppose we run a small book library and for now we are selling Programming Books. Within a few months we got popularity and getting many request for storing Mathematic Books. In future we may have much more requests for more genres. That is really good for the business.

So here we don’t know beforehand what we may need in future. So if we write code only taking care of programming related books selling logics then we may encounter to refactor code again. Even we may need to write entire logics again for other genres. That is really bad 🙁

So let’s jump into the code and see how can solve above discussed problem:

First we will create an abstract class that will contain a factory method like below:

abstract class BookFactory
{
    abstract public function createBook($type): GenericBook;
}

This class is basically called Factory Class. Since it’s containing an abstract method so the class that will be derived from it will must implement this method & follow the signature. It means the return type of createBook method will be an interface of GenericBook.

Now let’s create a sub-class that is basically Factory Concrete Class. The class which is responsible for creating object of Product Concrete class.

class Book extends BookFactory
{
    protected $name, $author;

    public function __construct($name, $author)
    {
        $this->name = $name;
        $this->author = $author;
    }

    public function createBook($type): GenericBook
    {
        try {
            return new $type($this->name, $this->author);
        } catch (\Throwable $th) {
            throw $th;
        }
    }
}

So all is going good, we have implemented createBook method with proper signature & this is a constructor method for setting up basic things. We are creating object here on fly on the run time, beforehand we don’t know that object we really want to create but as long as it’s returning GenericBook interface all is good. Through the interface we can enforce the Product Concrete Class to implement required methods.

Let’s create the interface:

interface GenericBook
{
    public function bookName(): string;

    public function bookAuthor(): string;

    public function price();
}

Now let’s create Product Concrete Class that contains logics about particular genre books.

class ProgrammingBook implements GenericBook
{
    private $bookName, $bookAuthor;
    private $price = '$120';

    public function __construct($name, $author)
    {
        $this->bookName = $name;
        $this->bookAuthor = $author;
    }

    public function __get($name)
    {
        echo $name . ' prop is not available, try method';
    }

    public function bookName(): string
    {
        return $this->bookName;
    }

    public function bookAuthor(): string
    {
        return $this->bookAuthor;
    }

    public function price()
    {
        return $this->price;
    }
}

Now our code is entirely following Factory Method Design Pattern and is loosely coupled. Below way client will use the class:

$book = new Book('PHP Book', 'Rasmus Lerdorf');
$programmingBook = $book->createBook('ProgrammingBook');
echo $programmingBook->bookName() . '<br>';
echo $programmingBook->bookAuthor() . '<br>';
echo $programmingBook->price() . '<br>';

This way client may create as many programming book as they need.

Now If we need more genre books then simple we can create another class and implement all logics to that particular class and don’t need to touch anything else. Even we can take advantage of common things that already have on the ProgrammingBook class.

Here is how to achieve it:

class MathBook extends ProgrammingBook
{
    private $price = '$90';

    public function price()
    {
        return $this->price;
    }
}

Supposed we need to sell Math books & we have all things same as programming book but price is different, so we can simple create a class for MathBook and extends ProgrammingBook. Since price is different we can override from particular class, that is really cool 😀

That’s all for the Factory Method, don’t forget to comment your recommendation/suggestion.