What does the variable $this mean in PHP?

It’s a reference to the current object, it’s most commonly used in object oriented code.

Example:

<?php
class Person {
    public $name;

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

$jack = new Person('Jack');
echo $jack->name;

This stores the ‘Jack’ string as a property of the object created.

Leave a Comment