Home » Programming » PHP » PHP $this Variable – What It Is, How To Use It [Examples]

PHP $this Variable – What It Is, How To Use It [Examples]

This article will explain the PHP $this variable – what it is, what it does, and how to use it – with code examples.

PHP Objects and Classes

PHP is an object-oriented programming language.

Code and data can be stored in an object which represents the items your code represents with a set of properties and methods.

For example, you might have objects that represent sales invoices, or cars, or pets, or people – each containing code and data relevant to the thing they represent.

The properties or attributes of an object are the data that describe it – you may have properties for a person object’s address, age, and height, for example.

The methods of an object are the functions that the object can call to make it do things in your code – for example a car object might have a drive() method that makes it move forward in a game.

Classes are specifications for an object – they define what an object should look like – what properties it can hold, and what it can do – the methods that it can call.

We will define a class and create an object based on it in the example below.

What is $this

The PHP $this variable is a special variable, reserved for when working with objects. When you are working within an object, a call to $this will return the current object.

$this is scoped to the current object and is unavailable outside of it.

Using $this in PHP

Below is a simple example of using $this in PHP:

<?php

class Animal {
    public $species;

    function __construct($species) {
        $this->species = $species;
    }

    function mutate(){
        $this->species = 'Monkey';
    }
};

$pet = new Animal('Elephant');
echo $pet->species;
$pet->mutate();
echo $pet->species;

In the above code:

  • A class Animal is defined, that has an property for the animal’s species
  • The class constructor, which sets the initial values, uses $this to set the property value based on the one supplied
  • An additional function, mutate(), uses $this to update the value of the species

This class and the methods are then demonstrated – a pet elephant object is created, and then mutated into a monkey.

In all cases above, $this* refers to the current object from which it is called within.

Any attempt to refer to $this outside of an object will result in the following error:

Fatal error: Uncaught Error: Using $this when not in object context

Why is $this Useful?

$this is something you will use constantly when working with PHP objects, as it accesses the current object, including its properties, you will use it in your object methods to manipulate property values, make calculations using property values, or even call other methods from within an object.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment