PHP OOP - $this calling object Tutorial

In this PHP tutorial we learn how to reference the object that a class member is using through the $this keyword, which helps us minimize errors in our classes.

How to use $this

The $this keyword is used to refer to the calling object. As an example, let’s consider a function that identifies a user by their name.

Example:
<?php
    class Person {
        // Attributes
        var $name = "Unknown";

        // Methods
        function introduction($username) {
            echo "Hi, my name is " . $username;
        }
    }

    $person1 = new Person();
    $person1->name = "John";
    $person1->introduction($person1->name);

?>

The introduction() method in the example above is intended to identify the person object by their specific name. When we call the method, it will print “Hi, my name is John”.

The method is doing what it’s supposed to, provided that the programmer input the correct name as an argument. If the programmer were to enter something else as the value, the method would no longer function as intended.

This could also cause serious security vulnerabilities in some situations.

Example:
<?php
    class Person {
        // Attributes
        var $name = "Unknown";

        // Methods
        function introduction($username) {
            echo "Hi, my name is " . $username;
        }
    }

    $person1 = new Person();
    $person1->name = "John";
    $person1->introduction("Jane");

?>

In the example above, the method will print “Jane” as the name, instead of “John”, which is not what we want.

To fix this problem PHP provides us with the $this keyword, which is used to refer to the object that’s calling the method. We can directly use the $name attribute inside the method by using $this->name

Example:
<?php
    class Person {
        // Attributes
        var $name = "Unknown";

        // Methods
        function introduction() {
            echo "Hi, my name is " . $this->name;
        }
    }

    $person1 = new Person();
    $person1->name = "John";
    $person1->introduction();

    echo "<br>";

    $person2 = new Person();
    $person2->name = "Jane";
    $person2->introduction();

?>

When we run the example above it will print the correct name each time the method is called.

Essentially, what’s happening is that PHP is replacing the $this keyword with whatever the object variable name is.

Example:
$person1 = new Person();
$person1->name = "John";
$person1->introduction($person1->name);

The difference is that the programmer can no longer make a mistake by entering the wrong name. It will always use the name that has been assigned to the object.

The $this keyword helps make our code shorter, easier to read, and less prone to errors and vulnerabilities.

Summary: Points to remember

  • The $this keyword refers to the calling object. It’s essentially a temporary variable that is replaced with the object name to refer to itself.
  • $this is used with the -> operator to reference a member. The member cannot have a $ symbol as prefix when being referenced.