DEV Community

Cover image for PHP OOP Part-1: Introduction, Object, and Class
Jamir Hossain
Jamir Hossain

Posted on

PHP OOP Part-1: Introduction, Object, and Class

In this series, I will cover the fundamentals of PHP Object-Oriented Programming (OOP). The content will be organized into sequential parts, each focusing on a specific topic. If you're a beginner or unfamiliar with OOP concepts, this series is designed to guide you step by step. In this part, I will discuss about the OOP introduction, object and class in PHP. Let's begin the journey of learning PHP OOP together!

Introduction

Software is essentially a program created to perform specific tasks on a computer. A program consists of several instructions written to achieve the program’s intended purpose. These instructions follow certain methods and styles, which are known as programming paradigms. There are several programming paradigms, such as:

  • Imperative
  • Declarative
  • Procedural
  • Functional
  • Object-Oriented (OOP), etc.

However, in this series, we will focus on understanding the Object-Oriented Programming paradigm in PHP.

PHP has supported Procedural Programming from the very beginning. Additionally, PHP can also be used for Functional Programming. But in 2004, with PHP version 5, OOP was introduced, allowing PHP to be used as an object-oriented programming language.

In this section, we will discuss the most important terms of Object-Oriented Programming System (OOP). We will try to understand each of these terms gradually.

What is an Object?

In our daily life, everything we see around us can be considered as an object. To identify objects more easily, we can think of them as nouns. For example, Man, Animal, Car, etc., are nouns, so we can refer to them as objects.

An object has various characteristics, such as:

  1. Properties:
    Properties of an object represent its attributes or features. For example, a car's properties could include its name, size, color, weight, etc.

  2. Actions/Behaviors/Methods:
    Actions, behaviors, or methods represent what an object can do. For instance, a car's behavior might include starting, stopping, or running.

Now for example, If we consider a Car object then we will find it's Properties and Behaviors.

  • Properties: color, size, weight, name, etc.
  • Behaviors: carCanStart(), carCanStop(), carCanRun(), etc.

Now the question is How to Create an Object in PHP? In PHP, to create an object, we first need to use a class. What is a class? We'll learn about that shortly. Meanwhile, consider the following example:

Code Example:

class Vehicle
{
   public $name;
   public $color;

   public function setValue(string $name, string $color)
   {
      $this->name  = $name;
      $this->color = $color;
   }

   public function getValue()
   {
      echo "Car name: $this->name\n";
      echo "Car color: $this->color\n";
   }
}

$toyota = new Vehicle;
$toyota->setValue('Toyota', 'Red');
$toyota->getValue();
Enter fullscreen mode Exit fullscreen mode

In this example, we can see that we have created an object named $toyota using a class called Vehicle. Here, to create the object, we used the new keyword. This is a built-in PHP keyword used to create a new object from a class.

In this way, we can create as many objects as needed. As mentioned earlier, an object should have certain characteristics. In our example, we can observe that the created object contains these characteristics, such as having properties and methods (or actions), among other features.

What is a Class?

A class is a template or blueprint which is used to create objects. When we create a class, we define its properties and methods, which can be used when objects are created of that class. Here’s an example of a simple class.

Code Example

class Car
{
   public $name;
   public $color;

   public function setValue(string $name, string $color)
   {
      $this->name  = $name;
      $this->color = $color;
   }

   public function getValue()
   {
      echo "Car name: $this->name\n";
      echo "Car color: $this->color\n";
   }
}

$toyota = new Car;
$toyota->setValue('Toyota', 'Red');
$toyota->getValue();

$tesla = new Car;
$tesla->setValue('Zip', 'Blue');
$tesla->getValue();
Enter fullscreen mode Exit fullscreen mode

In the code above, we see that using the Car class, we created two objects. This means that with the same class, we can create many objects (such as many car objects). In this sense, the Car class works like a blueprint.

I hope you now have a basic understanding of the concepts discussed in this section. In the next lesson, we will continue to explore more about Object-Oriented Programming.

Top comments (0)