What is the $this keyword and how to use it in PHP

Updated January 10, 2021・2 minute read Post By bobbyiliev#php #laravel #php oop #php $this
What is the $this keyword and how to use it in PHP

Introduction

The $this keyword is one of the most important and helpful things in PHP OOP(Object Oriented Programming). And unfortunately, it's one of those things that get overlooked. Many people may have heard of this keyword but don't know how to use it.

In this post, I am going to briefly explain what the $this keyword is and how it works. If you don't have any knowledge of OOP, then I would recommend that you go and check out what it is so that you can understand this post even better.

What is the $this keyword, and how does it work

The $this keyword lets us use our class methods and properties. This means that we can, for example, call a method or a property within the current class itself.

As an example, let's start by creating a simple class called Dog. We are going to use this class for the next examples in our post.

class Dog {

    public $name;
    public $age;
}

So in order to get our classes property, we have to use the $this keyword as shown below:

$this->name

The example above simply shows how to use the keyword properly. Notice how we have to remove the $ sign from the $name property. It's crucial that you remember this because you can get confused about why your code doesn't run properly.

The $this keyword and methods

Now let's use the $this keyword in a method inside our class:

class Dog {

    public $name;
    public $age;

    public function intro(){

        echo 'This is ' .$this -> name . ' and it is ' .$this -> age . ' old.';

    }
}

For our function to run, we have to create ourselves an object from the class and set the values for our class's properties, and then call out the public method:

<?php
// Instantiating the object
$toffee = new Dog();

// Set the name property
$toffee->name = 'Toffee';

// Set the age property
$toffee->age = '2 months';

And finally, we have to call our function.

Checkout our latest product - the ultimate tailwindcss page creator 🚀
echo $toffee->intro();

What we did now get the class properties into our function. The output will be something like this:

This is Toffee and he is 2 months old.

We can also add a method into a method inside of our class whilst using the $this keyword. Here is a quick example for which we are going to use a new class.

class Fun{
    public $name;

    public function say(){
         echo 'Hello ' . $this->name;     
    }

    public function hello(){
        $this->say;
    }
}

Now let's create our object, set the values, and echo out the function again.

// Instantiating the object
$sammy = new Fun();

//  Set the name property
$sammy->name ='Sammy';

//  Call the hello method
echo $sammy -> hello();

The outcome should be

Hello Sammy

This is how to use the $this keyword in a method and call another method of that class.

Conclusion

As you can see overall, it is quite handy. Of course, it is going to take some time to get used to putting it in your code, but after you get the hang of it, you will be using it 24/7.

If you are just getting started with PHP, I strongly recommend this course on PHP basics here:

Getting started with PHP

Hope that this helps!

Still have a question?

Get help or discuss anything development/design related.

Start a discussion