Please wait

What is Object-Oriented Programming?

It's time to tackle one of the tougher subjects in programming. This lesson will dive mostly into theory. Subsequent lessons will cover syntax and practical examples. So, what is OOP?

Programming paradigms

The first thing to understand is the idea of a programming paradigm. A programming paradigm is a style, or "way," of programming. Some languages allow multiple paradigms (like Python, which allows both procedural and object-oriented programming), while others are more specific (like Haskell, which is purely functional). Each paradigm has its own unique way of organizing code and accomplishing tasks.

While there are quite a few programming paradigms, we're going to focus on two called procedural programming and object-oriented programming.

Procedural Programming

Procedural programming is a type of imperative programming where the program is built from procedures. The procedures, also known as routines, subroutines, or functions, simply contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution, including by other procedures or itself.

For example, a simple procedural program in PHP might look like this:

function add_numbers($num1, $num2) {
  return $num1 + $num2;
function subtract_numbers($num1, $num2) {
  return $num1 - $num2;
$result = add_numbers(5, 3)
echo $result; // Outputs: 8
$result = subtract_numbers(5, 3)
echo $result; // Outputs: 2

Problems with procedural programming

While procedural programming can be straightforward and effective for small, simple programs, there are several reasons why it might not be the best choice for larger, more complex applications:


Procedural programming does not scale well. As a program gets larger and more complex, it becomes more difficult to keep track of all the variables and functions in the program. If not carefully managed, procedural programming can lead to "spaghetti code" - code that is tangled and difficult to understand or modify.

For example, in a large PHP script, you might have hundreds or even thousands of variables and functions. If you aren't careful, you might accidentally create two functions with the same name, causing a conflict:

function doSomething() {
    // Code here...
// Many lines of code later...
// Oops! This function has the same name as the function above.
function doSomething() {
    // Different code here...

Data Exposure

In procedural programming, data can be accessed and modified from anywhere in the program, which can make debugging difficult and can lead to an inconsistent program state.

For example, in PHP, you might have a variable $username:

$username = "John Doe";
// Many lines of code later...
$username = 1234; // Oops! Accidentally changed the value of $username.

Code Reusability and Organization

Procedural programming does not inherently encourage code reusability. While you can certainly write reusable functions in a procedural program, object-oriented programming languages provide structures like classes and objects that make it easier to encapsulate related data and behavior in one place and reuse it throughout your program.

For instance, consider a scenario where you're working with user profiles in a procedural PHP script:

$user1_name = "Alice";
$user1_email = "";
$user2_name = "Bob";
$user2_email = "";
// to display user info
function displayUserInfo($name, $email) {
  echo "Name: " . $name;
  echo "Email: " . $email;
displayUserInfo($user1_name, $user1_email);
displayUserInfo($user2_name, $user2_email);

As you can see, this approach can quickly get messy and difficult to manage as you add more users. In an object-oriented approach, these problems can be avoided.

Object-oriented Programming (OOP)

Object-oriented programming is a programming paradigm that is based on the concept of "objects". An object is a data structure that contains data (known as attributes or properties) and procedures (known as methods). Objects are instances of classes, which can be considered blueprints for creating objects. The idea behind OOP is to bundle these attributes and methods that are related to each other into individual objects.

Sound confusing? Let me break down what all this means.

Understanding objects

Imagine things around you, like your phone, laptop, or car. Also, think about things you can't touch but know exist like your bank account or the money transfer you just made.

All these things have two main features:

  • State
  • Behavior

Let's take your bank account as an example. Its condition or 'state' includes:

  • Account number
  • Balance

Your bank account also can do certain actions or 'behaviors', such as:

  • Add money (deposit)
  • Take out money (withdraw)

In PHP programming, we create 'objects' that work pretty much the same way. An 'object' keeps its condition or 'state' in special variables called 'properties'. It also can do certain actions or 'behaviors' using functions, which we call 'methods' when they're in an object.

Understanding classes

The question is, how do we create an object? We can do so with a class!

Think of a class in PHP as a blueprint or recipe. This blueprint defines how to make a certain type of object like a cookie recipe defines how to make a certain type of cookie.

For instance, if you had a class for a 'Car', the class would contain all the common things that cars have - such as color, make, and model - and things that cars do - such as drive, stop, or honk. These common things are called properties, and the things that cars do are called methods.

When you make an actual car from this blueprint, you're creating an 'object' of the 'Car' class. So, you might create a red car, a blue car, or a green car. Each of these cars is an object, and they're all created based on the 'Car' class blueprint.

So, in simple terms, a class is like a blueprint for creating objects in PHP.

Understanding properties

I've brought up the words "properties" and "methods." Let's break those words down too. Properties in a class are like the characteristics or attributes of an object. Just like a real-world car can have properties like its color, make, or model, a 'Car' class in PHP could have properties like $color, $make, or $model.

Let's continue with the car example. If we have a 'Car' class, the car's color would be a property. So if you have a red car, "red" is the value of the color property for that particular car object.

In simple terms, properties in a class are like the details or attributes that define what an object is or has.

Understanding methods

Methods in a PHP class are like the actions or things that an object can do. They're like the verbs to the noun that an object represents.

Using the car analogy again, a 'Car' class might have methods like drive(), stop(), or honk(). These methods represent the actions that a car can take. For example, the drive() method might be used to change the car's location, or the stop() method might be used to halt the car.

So, in simple terms, methods in a PHP class are the actions or behaviors an object can perform.

What to expect

In the next set of lessons, we're going to look at how to create classes, objects, properties, and methods. Once we do, we'll have a basic foundation of OOP, which will allow us to dive into more complex topics, such as encapsulation, polymorphism, dependency injection, design patterns, and other interesting subjects.

Key takeaways

  • Object-Oriented Programming (OOP) is a coding style centered around 'objects'. These objects are created from 'classes', which are blueprints for creating an object.
  • A Class in PHP is like a blueprint for creating objects. It describes what kind of data the object can hold and what actions the object can do.
  • Objects are instances of a class. For example, if you have a class 'Car', an object would be a specific car like a red Toyota or a blue BMW.
  • Properties in a PHP class are the attributes or characteristics of an object, like the color, make, or model of a car. You can think of them as the object's variables.
  • Methods in a PHP class represent the actions that an object can take. They're like functions that belong to an object. For example, a car might have methods like drive(), stop(), or honk().


Please read this before commenting