What is self keyword in PHP

In PHP, you use the self keyword to access static properties and methods.

Below is the example to expalin self

Refer this code:

class ParentClass {
function test() {
self::demo();

}

function demo() {
echo ‘Example of self !’;
}
}

class ChildClass extends ParentClass {
function demo() {
echo ‘Return chlid class’;
}
}

$obj = new ChildClass();
$obj->test();

In this example, self::demo() will always output ‘Example of self !’.
Now we can see that self refers to the class in which it is called.
So, you should use self only when $this is not available, or when you don’t want to allow descendant classes to overwrite the current method.

If we use $this in test method it will return “Return chlid class”;