Php this keyword

What is this keyword in Php?

this is a reference variable that refers to the current object.
OR
The this keyword can be used to refer current class instance variable.

Understanding the problem without this keyword on method


<?php 
class MyClass {
	function MethodA() {
		echo "Hello";
	}

	function MethodB() {
		MethodA();
	}

}
$mc = new MyClass;
$mc -> MethodB();
?>
Fatal error: Call to undefined function MethodA()

With this keyword on the method


<?php 
class MyClass {
	function MethodA() {
		echo "Hello";
	}
    function MethodB() {
		$this->MethodA();
	}

}
$mc = new MyClass;
$mc -> MethodB();
?>
Output:- Hello

Understanding the problem without this keyword on variable


<?php 
class stuClass {
    public $stu_name="ramesh";
	public $stu_rollno="Btech";

    public function display() {
        echo $stu_name.' in '.$stu_rollno;
    }
}
$sc=new stuClass;
$sc->display();
?>
Notice: Undefined variable: stu_name
Notice: Undefined variable: stu_rollno

with this keyword on variable


<?php 
class stuClass {
    public $stu_name="ramesh";
	public $stu_rollno="Btech";

    public function display() {
        echo $this->stu_name.' in '.$this->stu_rollno;
    }
}
$sc=new stuClass;
$sc->display();
?>
Output:- ramesh in Btech

this keyword is not use for static variable and method

this keyword is not used for static variable and method in that situation we use self keyword which is define in the self keyword page.


<?php  
class stuClass {
    public static $stu_name="ramesh";
	public static $stu_rollno="Btech";

    public static function display() {
        echo $this->stu_name.' in '.$this->stu_rollno;
    }
}
$sc=new stuClass;
$sc->display();
?>
Fatal error: Using $this when not in object context