In the previous tutorials, we used functions to write PHP scripts. Therefore, it is called structural programming. But, structural programming becomes more messy and difficult to understand and handle as the size of the application or project increases. We are now starting Object Oriented Programming- OOP in PHP that makes use of classes and objects.

What is Object-Oriented Programming – OOP in PHP

OOP is an abbreviation of Object-Oriented Programming. As the name suggests, it contains objects that hold both the data and properties.

It depends on the requirements to select out of OOP in PHP or structural programming in PHP. However, in most cases, OOP outperforms structural programming. The reasons due to which OOP in PHP is more beneficial are below.

  • OOP is easier to understand and fasater to execute.
  • Code become in a structure which make it able to moodify easily.
  • Using OOP in PHP, we can avoid from repetetions of code.
  • Following standard OOP principas, we can develop both small scale and large scale applications. We will learn these standard features in the next tutroials.

What is a class in OOP?

A class is the main starting point of OOP in PHP. A class contains the attributes, functions, and specifications. For instance, look at the following examples to imagine what a class actually is about.

  • A class of pet animals
  • A class of Cars

As in the above classes, we have classified the animals in the Animal class and cars in the Car class. This class contains attributes and functions, which we will later in the article.

What are Objects in OOP?

Objects are instances of the class that holds or reflects its properties. There can be multiple objects of a class that reflect the same properties of its class. However, each object can have different values for the properties of its class.

In the above example of the class of pet animals and cars, we can have the following objects.

  • Object of class pet animals containing cat and dog.
  • Similarly, object of class Cars containing Honda and Toyota.

Note: An object inherits all the properties of its class.

How to create a class in PHP OOP?

The class keyword is used to declare a class in PHP. The name of the class comes after this keyword and it is recommended to start the class name with a capital letter. For instance, we create a class of pet animals in PHP.

<?php
class Pets {
  // code
}
?>

Now, we have a class Pets, that contains no function, attributes, or code. In the next example, we will fill the PHP class with some functions. Let’s say, we have the name of the pet and its color.

<?php
class Pets {

  // Properties

  public $nick_name;
  public $color;

  // functions
  function set_nick_name($name) {
    $this->nick_name = $name;
  }

  function get_nick_name() {
    return $this->nick_name;
  }
}
?>

Note: Variables in class are also known as attributes and function as methods.

How to create an Object of class in PHP?

To create a new object in PHP, use new keyword and write the name of the object after the keyword. Look at the following example, in which we create two objects of Pets class.

<?php
class Pets {
  // Properties
  public $nick_name;
  public $color;

  // Methods
  function set_name($name) {
    $this->nick_name = $name;
  }
  function get_name() {
    return $this->nick_name;
  }
}

$cat = new Pets();
$dog = new Pets();

$cat->set_name('yii');
$dog->set_name('hop');

echo $cat->get_name();
echo "<br>";
echo $dog->get_name();
?>
  • In the above example, we include two methods to set and get the name of the pet.
  • We use the new keyword to create two objects $cat and $dog.
  • Call the set_name method using both objects.
  • Now, both objects hold its own property of nick name.

Now, in the above example, we will add two more functions that will set and get the color property of the pet animal.



<?php
class Pets {
  // Properties
  public $nick_name;
  public $color;

  // Methods
  function set_name($name) {
    $this->nick_name = $name;
  }
  function get_name() {
    return $this->nick_name;
  }
  function set_color($color) {
    $this->color = $color;
  }
  function get_color() {
    return $this->color;
  }
}

$cat = new Pets();
$dog = new Pets();

$cat->set_name('yii');
$dog->set_name('hop');

$cat->set_color('brown');
$dog->set_color('white');

echo $cat->get_name();
echo $cat->get_color();

echo $dog->get_name();
echo $dog->get_color();
?>

  • In the above example, we include two additional methods to set and get the color of the pet.
  • We use the new keyword to create two neobjects $cat and $dog.
  • Call the set_name and set color methods for both objects.
  • Now, both objects hold its own property of nick name and color.
  • We call the get method using both objects. It will output the name and color of both pets.

What is $this keyword in PHP OOP?

$this keyword refers to the current object in PHP OOP. The keyword can either be used to set the value for the current object or get the value.

There are two methods to update the value of the class attributes.

  1. Using $this keyword
  2. Set the value directly using object.

Look at the following example, in which we use $this keyword inside the function to set the value of pet color.



<?php
class Pets {

  // Properties

  public $nick_name;
  public $color;

  // functions
  function set_nick_name($name) {
    $this->nick_name = $name;
  }

  function get_nick_name() {
    return $this->nick_name;
  }
}
?>

Make sure, in the above example, the nick_name will be set for the object calling this function. In the next example, we will set the nickname without using $this keyword.

<?php
class Pets {
  public $color;
}
$cat = new Pet();
$cat->name = "brown";

echo $cat->color;
?>

PHP instnceOf function

You can use PHP instanceOf function to check the class of the object to which it belongs.

<?php
$dog = new Pet();
var_dump($dog instanceof Pet);
?>

You can refer to the official PHP 8 Classes and Objects article here.