OOP in PHP! The 4 Pillars.

Hello there, this week I will be taking a step back from my AWS blog series, and talk about Object Oriented Programming in PHP 7. PHP (aka Hypertext Pre-Processor) was created by Rasmus Lerdorf in 1994, and since it has been around a while, it has plenty of backing and documentation. PHP is a server-side scripting language, that is used to produce static or dynamic websites or web applications. PHP 7 is the newest version after PHP 5, which took its name after the failure of PHP 6 ability to implement Unicode. Like most Object-Oriented Programming languages we have many similarities and a couple of differences, so let’s discuss these variations.

What Is Object-Oriented Programming? In the beginning, all programming languages were procedural programming. While procedural programming was the core concept for the longest time, A guy by the name of “Alan Kay” decided it was time for something new, something revolutionary. Alan called his new concept to programming “Object-Oriented Programming” and based the language on classes and objects. When we take a look at the world around us we can conclude the world is full of objects, like the sun, earth, moon, houses, people. Every object has its properties like color and shape, they also can perform certain actions like move or eat, and furthermore these objects can interact with each other. What about classes? With our idea of objects in mind, we can simply say some objects are the same, but they have different properties. For example, a cat needs to eat, purr, walk, sleep, but their properties like color and fur or their name may be different, so we simply say, same objects belong to one class. What does this mean in Object-Oriented Programming? OOP is a programming style that focuses on using objects and their interactions to design and build applications, based on real-world objects and their behaviors.

Classes and Objects: While Classes don’t technically exist in every programming language they do have a purpose, unlike in Javascript where everything is an object, and have similar meanings. A class is a blueprint or template to build a specific type of object. An object is just a single instantiation or implementation of that class. Take a House class, for example, many House objects can be created from a class pattern or blueprint. With a class, we can specify just how to construct a house such as dimensions, materials, and layouts. Using this blueprint we get an idea of just how to build the house object. When using classes to build our house, we have a strict framework in which to build upon, but what if we didn’t want our house object to all have the same look or color?

Properties and Methods: These different characteristics we want to define in or class or object, are called “properties”. Some properties can be changed like the color or layout and others do not, depending on your needs. Properties are exactly like typical variables, but they are only related to the class, and can only be accessed by using the object or class name. If you are a programmer I’m sure you have heard the word function before, which is a procedure or behavior. When a function becomes part of a class, we refer to that function as a method. Methods are extremely useful, and can also access the properties of a class. For example, in our class of House, we want to create a method called paintHouse(). We can now look to the objects’ color property to decide just what color to paint this instance of a house. The methods and properties of a class are referred to as members.

Encapsulation: Keeping our House class as the example, say we have a property of an address with a value of its address. With a simple method call, we could change the value of the property which is not good practice. To avoid this we simply declare the property as private. private $address = “123 Some St.”;

Inheritance: Inheritance is just as the name implies, which is something passed on to the child from the parent, in this case, properties and methods. We start with a base class House and by extending it to a child class of Villa, it can inherit all its parents’ properties and methods. From here he can give the child class, or Villa, it’s own separate methods and properties, like more bay windows.

Abstraction: Abstraction is also like the name itself implies, It is abstract and not quite clear of its intentions, like an abstract painting. Let’s say we have a parent class of Vehicle which branches off into two subclass Transport Vehicle and Passenger Vehicle. Furthermore, Transport vehicle extends into two subclasses of Van and Box Truck, and Passenger vehicle extends into two subclasses of Car and Motorcycle. Now we need the classes Vehicle > Transport Vehicle / Passenger Vehicle for inheritance but we don’t want anyone making instances of these abstract objects. If we need to make these classes un instantiation we can simply set them to abstract by adding the keyword abstract, it’s that simple. Ex: abstract class Vehicle{

Polymorphism: Also known as Interface in PHP, similar to abstraction but allows us to declare 0 or more methods without providing the implementation of those methods. An abstract class has a parent-child relationship with the class that extends from it, and cannot extend to another class. This means PHP is a single parent language, which means a class can have any one direct class, and no more. In an interface, there is no parent-child relationship, instead, a child class Implements from a class and then can extend to another parent class. Interfaces lie outside the hierarchy, so classes from different inheritance can implement the same interface. In a broad sense this a way for two, unlike objects to communicate.

I hope you enjoyed my breakdown of Object-Oriented Programming for PHP. We touched on Classes / Objects, Properties / Methods, and the 4 pillars of OOP which are: Encapsulation, Inheritance, Abstraction, and Polymorphism. Thank you and stay tuned for more.

Flatiron Graduate, Full-Stack developer on his job search. I love working with Javascript and React.