The 4 pillars of OOP with code samples in PHP

Alin Pintilie
3 min readJan 13, 2022

--

Photo by Kate Hliznitsova on Unsplash

After more than three years of coding and more than five in the IT field, after some pet projects and some courses, I’ve started to assess myself by asking some questions and I realized that there are few essential concepts without which it is impossible to assume that you are a good programmer. One of them is the OOP Concepts and methodology. If you are already coding for a few years and use objects every day but you won’t be able to enumerate and define the concepts of OOP or to explain when you should use an interface rather than an abstract class you should act now. Moreover, I think that the lack of fundamental theoretical knowledge is a permanent source of technical debt for all companies. I’ve just finished reading the book “The object oriented thought process” (Matt Weisfeld) and I’ve decided to write down for myself some ideas from this book. I will put them here and I hope it will be a series. If you find them useful, just stay with me.

I am going to write with supposition that this is not an explanation of what is OOP but an outline of few concepts.

There are three generally accepted pillars (encapsulation, inheritance, polymorphism) and some are including the forth (composition).

Encapsulation

Objects have attributes and behaviors but some of these must be private. So this is encapsulation. When you design a class you should know that you have to keep public only those attributes and behaviors essential for other objects , the others go private.

What is designed to be public will serve as an interface for an object and the hidden part we call it implementation.

Inheritance

Inheritance allows us to inherit attributes and methods of another class. A class that contains all the attributes and methods that are common to the classes that inherit from it is called superclass , parent class or base class. As you already know we call it subclass, child class or derived class the class which extends the first one. The child and the parent class are in the “is a” relationship meaning that child is a kind of a parent.

Polymorphism

When the same message is sent to two similar objects the answer will be different for each of them. This property is strictly correlated with inheritance. Two classes having the same parent could implement or override a certain method providing two different responses.

Composition

The possibility of building an object containing other objects is called composition. This concept is representative of “has a” relationship. There are two types of composition: association and aggregation. Aggregation is met when an object is composed of other objects (a laptop is composed of processor, display, RAM and other components), meanwhile association is the relationship between many independent objects that compose a system (a desktop needs a monitor, a keyboard, a mouse and other ). As Matt Weisfeld suggests, “an aggregation is a complex object composed of other objects. An association is used when one object wants other object to perform a service for it”

--

--