Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

$this $this

<?php

class x {
    private $p = 'Yes!';
    
    function foo() {
        // valid code
        echo $this->$this;
    }
    
    function __toString() {
        return 'p';
    }
}

(new x)->foo();

$this is the pseudo-variable that represents the current object. It is also a variable, like others, and may be used to describe a dynamic property.

Since a property must be described as a string, it is possible to use the magic method __toString() to convert the object into a string, and map an existing property.

See Also

PHP Features

Last updated: 10 September 2026