$this keyword in PHP

$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.

Example 1: A simple program to show the use of $this in PHP.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class simple{
  
    public $k = 9;
  
    public function display(){
        return $this->k;
    }
}
  
$obj = new simple();
echo $obj->display();
  
?>

chevron_right


Output:

9

$this – a pseudo-variable: Unlike other reserved keywords used in the context of class like the static, parent, etc does not need to be declared with the dollar sign (‘$’). This is because in PHP $this is treated as a pseudo-variable.
In PHP, this is declared like a variable declaration (with the ‘$’ sign) even though it is a reserved keyword. More specifically, $this is a special read-only variable that is not declared anywhere in the code and which represents a value that changes depending on the context of program execution.

Example 2: A program that updates the value of a variable of a specific object using $this keyword.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class simple {
  
    public $k = 9;
  
    public function change($val){
        $this->k = $val;
    }
  
    public function display(){
        return $this->k;
    }
}
  
$obj = new simple();
print("value of before update: ");
echo $obj->display();
  
$obj->change(8);
print("value of after update: ");
echo $obj->display();
?>

chevron_right


Output:

value of before update: 9
value of after update: 8

As of PHP 7.0.0, calling a non-static method statically from an incompatible context results in $this being “undefined” to the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been deprecated (even if called from a compatible context). Before PHP 5.6.0, such calls already triggered a strict notice.

Example 3: In this example, $this keyword becomes “not defined” when a non-static method is called in the context of a static one.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
class A {
  
    function foo() {
  
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
  
class B {
  
    function bar() {
        A::foo();
    }
}
  
$a = new A();
$a->foo();
  
A::foo();
  
$b = new B();
$b->bar();
  
B::bar();
?>

chevron_right


Output:

$this is defined (A) 
$this is not defined. 
$this is not defined. 
$this is not defined.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.