When to use self over $this in PHP ?

The self and this are two different operators which are used to represent current class and current object respectively. self is used to access static or class variables or methods and this is used to access non-static or object variables or methods.
So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.

self operator: self operator represents the current class and thus is used to access class variables or static variables because these members belongs to a class rather than the object of that class.

Syntax:



self::$static_member

Example 1: This is the basic example which shows the use of self operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
class GFG {
    private static $static_member = "GeeksForGeeks";
  
    function __construct() {
        echo self::$static_member;
        // Accessing static variable
    }
}
  
new GFG();
?>

chevron_right


Output:

GeeksForGeeks

Example 2: This example is a demo of exploiting polymorphic behavior in php using self.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
class GFG {
    function prnt() {
        echo 'Parent Class';
    }
  
    function bar() {
        self::prnt();
    }
}
  
class Child extends GFG {
    function prnt() {
        echo 'Child Class';
    }
}
  
$parent = new Child();
$parent->bar();
?>

chevron_right


Output:

Parent Class:

Here the parent class method runs because the self operator represents the class, thus we see that the main class method is the method of the parent class only.

$this operator: $this, as the ‘$’ sign suggest, is an object. $this represents the current object of a class. It is used to access non-static members of a class.

Syntax:

$that->$non_static_member;

Example 1: This is the basic example which shows the use of $this operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class GFG {
    private $non_static_member = "GeeksForGeeks";
  
    function __construct() {
        echo $this->$non_static_member;
        // accessing non-static variable
    }
}
  
new GFG();
?>

chevron_right


Output:

GeeksForGeeks

Example 2: This example is a demo of polymorphic behavior in php using self.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
class GFG {
    function prnt() {
        echo 'Parent Class';
    }
  
    function bar() {
        $this->prnt();
    }
}
  
class Child extends GFG {
    function prnt() {
        echo 'Child Class';
    }
}
  
$parent = new Child();
$parent->bar();
?>

chevron_right


Output:

Child Class

Here, there is no reference to any class and the object which is pointing the child class is calling the method defined in the child class. This is an example of dynamic polymorphism in PHP.



My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

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.