April 22, 2025

self and this are both keywords used in object-oriented programming to refer to the current class or object, respectively. However, there are some differences between the two:

  1. Usage: self is used to refer to the current class, while this is used to refer to the current object.
  2. Scope: self is a static keyword that is resolved at compile-time, while this is a dynamic keyword that is resolved at runtime.
  3. Accessing static properties and methods: self is used to access static properties and methods of the class, while this is used to access instance properties and methods.
  4. Naming conflicts: self can be used to disambiguate between class-level and instance-level properties and methods that have the same name, while this is not used for this purpose.

Here’s an example that illustrates the usage of self and this:

class MyClass {
    private static $count = 0;
    private $name;

    public function __construct($name) {
        $this->name = $name;
        self::$count++;
    }

    public function getName() {
        return $this->name;
    }

    public static function getCount() {
        return self::$count;
    }
}

$obj1 = new MyClass('John');
$obj2 = new MyClass('Jane');

echo $obj1->getName(); // Output: John
echo $obj2->getName(); // Output: Jane
echo MyClass::getCount(); // Output: 2

In this example, the MyClass class has a static property $count that is incremented each time a new object is created using the __construct() method. The getName() method returns the name of the object, while the getCount() method returns the total number of objects created.

Inside the __construct() method, the $name property is set using this, while $count is incremented using self. The getCount() method also uses self to access the $count property.

Overall, self is used to refer to the current class and its static properties and methods, while this is used to refer to the current object and its instance properties and methods.

$this refers to the instance of the class, that is correct. However, there is also something called static state, which is the same for all instances of that class. self:: is the accessor for those attributes and functions. $this refers to the current object,

self refers to the current class. The class is the blueprint of the object. So you define a class, but you construct objects.

So in other words, use self for static and this for none-static members or methods.
self is used at the class-level scope whereas $this is used at the instance-level scope. $this is used to reference methods and properties of the current instance of a class.

self us used to reference static methods and properties, shared by all instances (and even accessible outside of any instance) of a class.
self refers to the calling object’s class. $this refers to the object itself. Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.
self (not $self) refers to the type of class, where as $this refers to the current instance of the class. self is for use in static member functions to allow you to access static member variables. $this is used in non-static member functions, and is a reference to the instance of the class on which the member function was called.

Because this is an object, you use it like: $this->member

Because self is not an object, it’s basically a type that automatically refers to the current class, you use it like: self::member

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *