The pillars of Object Oriented Programming (OOP)

Andrei Birta
3 min readDec 29, 2022

--

Object-oriented programming (OOP) is a programming paradigm that is based on the concept of “objects”, which can contain data and code that operates on that data. OOP languages are designed to encapsulate data and the functions that operate on that data within a single entity, or object.

Encapsulation

Encapsulation is a fundamental concept in object-oriented programming (OOP). It relates to the concept of integrating data and the functions that are performed on that data into a single object.
Encapsulation helps to reduce complexity by creating a clear separation between the internal representation of an object and the way it is used. It also allows for data and functions to be protected from outside through access modifiers, which can help to maintain the integrity and consistency of the data.
To implement encapsulation within a programming language, objects may have properties (data) and methods (functions). The properties and methods of an object are generally declared as private, meaning that they can only be consulted and modified within the object itself. This will enhance the data and function encapsulated in the object.

class Person {
private $name;

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

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

$person = new Person("John Doe");
echo $person->getName(); // John Doe

In the example from above, the property “name” is not accessible outside of the class directly only through the public function “getName()”.

Abstraction

It refers to the idea of exposing only the essential characteristics of an object, while hiding the details of the implementation.
Developers may concentrate on what an object does rather than how it does it thanks to abstraction. By hiding an object’s internal implementation and presenting a crystal-clear, straightforward interface for dealing with it, it also aids in reducing complexity.


abstract class Shape
{
abstract public function area();
}

class Rectangle extends Shape
{
private $width;
private $height;

public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}

public function area()
{
return $this->width * $this->height;
}
}

class Square extends Shape
{
private $side;

public function __construct($side)
{
$this->side = $side;
}

public function area()
{
return $this->side * $this->side;
}
}

$rectangle = new Rectangle(10, 5);
echo $rectangle->area(); // 50

$side = new Square(5);
echo $side->area(); // 25

In this example, from above, the Shape class is an abstract class that defines the area() method. The Rectangle and Square classes are concrete classes that extend the Shape class and provide concrete implementations of the area() method. This allows the client code to work with shapes in a uniform way, without needing to know the specific implementation details of each type of shape.

Inheritance

Is the ability of one class to inherit the characteristics and properties of another class. Inheritance allows developers to create new classes and to get properties and functions from existing classes, this thing can help to reduce the amount of code that needs to be developed and maintained.

class Fruit {
public $nane;

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

public function baseMessage() {
echo "The fruit name is ".$this->name;
}
}

class Apple extends Fruit {

public function extendMessage() {
echo "Hello from Apple class";
}
}

$apple = new Apple("Red Apple");
$apple->extendMessage();
$apple->baseMessage(); // The fruit name is Red Apple

In this example the property name from the class Fruit will be accessible from the class Apple.

Polymorphism

Represents the ability of a function to have the same name but different implementations.
This concept of object-oriented programming is very useful because it helps us to optimize the code we write at the time of a development and it gets rid of the need for new functions, encouraging us to override an existing function, if there is a possibility from the perspective of the functionality it has to fulfill.

interface Message {
public function message();
}

class A implements Message {

public function message() {
echo "Hello form class A";
}
}

class B implements Message {

public function message() {
echo "Hello form class B";
}
}

Here we are using the same function name in both classes, but the content of the function differs from class to class.

Get more valuable insights and tips by following me!
Also check my list of articles about the PHP or different type of architecture.

--

--