No Change To $this

By Ondrej Mirtes

<?php

function foo(&$var) {
    $var = 2;
}

$a = 1;
foo($a);
var_dump($a); // 2

class A {
    public function do() {
        foo($this);
        var_dump($this); // still the same object
    }

    public function doo() {
        $a = new self();
        foo($a);
        var_dump($a); // 2
    }
}

Variable $this is passed and reassigned by reference is a noop, as opposed to all other variables. I’d expect an error to be honest.

See Also

PHP Features

Last updated: 14 July 2026