PHP $this Keyword: Syntax, Examples & Usage

Introduction

In PHP Object-Oriented Programming (OOP), the $this keyword refers to the current object of a class. It is commonly used inside class methods to access the properties (variables) and methods of the same class.

Whenever an object calls a method, PHP automatically assigns the current object reference to $this inside that method.

📖
The $this keyword in PHP is used to:
  • Refer to the current object
  • Access class properties (variables)
  • Call class methods
  • Distinguish class variables from local variables

Note: It is mainly used in non-static methods of a class.

Why Do We Use the $this Keyword?

When working with classes, we often need to access methods or variables defined inside the same class. If we try to call them directly without using $this, PHP will not recognize them as part of the object.

Therefore, $this helps PHP understand that the method or variable belongs to the current object.

Syntax of $this Keyword

The syntax of the $this keyword is very simple.

Accessing a Class Property


$this->variableName

Calling a Class Method


$this->methodName();

Here:

  • $this represents the current object
  • -> is the object operator
  • It is used to access properties and methods

Understanding the Problem Without $this Keyword (Method Example)

Let’s understand what happens when we try to call a method without using $this.

Example


class MyClass {
   function MethodA() {
       echo "Hello";
   }
   function MethodB() {
       MethodA();
   }
}
$mc = new MyClass;
$mc->MethodB();

Output

Fatal error: Call to undefined function MethodA()

Explanation

In the above example:

  1. We created a class MyClass.
  2. It contains two methods:
    • MethodA()
    • MethodB()
  3. Inside MethodB(), we tried to call MethodA() directly.

The problem is that PHP treats MethodA() as a global function, not as a class method.
Therefore, PHP throws an error because the function is not defined globally.

Using the $this Keyword to Call Methods

Now let’s correct the previous example using $this.
Example


class MyClass {
   function MethodA() {
       echo "Hello";
   }
   function MethodB() {
       $this->MethodA();
   }
}
$mc = new MyClass;
$mc->MethodB();

Output

Hello

Explanation

In this example:

  1. We used $this->MethodA() inside MethodB().
  2. $this refers to the current object.
  3. ->MethodA() calls the method belonging to the same object.

Therefore, PHP correctly calls MethodA() and prints Hello.

Understanding the Problem Without $this Keyword (Variable Example)

Now let’s see what happens when we try to access class variables without using $this.

Example


class stuClass {
   public $stu_name = "Ramesh";
   public $stu_rollno = "Btech";

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

Output

Notice: Undefined variable: stu_name Notice: Undefined variable: stu_rollno

Explanation

In this example:

  • The class contains two variables:
    • $stu_name
    • $stu_rollno

Inside the display() method, we tried to access them directly:


echo $stu_name.' in '.$stu_rollno;

PHP treats these as local variables, not class properties.

Since they are not defined inside the function, PHP generates an undefined variable notice.

Using $this Keyword with Variables

To correctly access class variables, we must use $this.
Example


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

Explanation

Here:

  • $this->stu_name accesses the class property stu_name
  • $this->stu_rollno accesses the class property stu_rollno
    Because $this refers to the current object, PHP correctly retrieves the values.

Important Points About $this Keyword

Here are some important rules about $this.

1. $this Refers to the Current Object

It always represents the object that calls the method.

2. $this Works Only Inside Class Methods

It cannot be used outside the class.

3. $this Cannot Be Used in Static Methods

Static methods belong to the class, not the object.

4. $this Is Used with the Object Operator

The object operator -> is required to access properties and methods.

Example:


$this->variable
$this->method()

5. $this Keyword Cannot Be Used in Static Methods

The $this keyword works only with object instances, not with static members.
Example



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();

Output

Fatal error: Using $this when not in object context

Explanation

In this example:

  • The variables and method are declared as static.
  • Static members belong to the class, not the object.

Since $this refers to an object, PHP throws an error.

Correct Way to Access Static Variables

For static variables and methods, we should use the self keyword instead of $this.

Example:


self::$variableName

This accesses the static member within the class itself.

Real-Life Example

Consider a Student class where each object represents a student.


name = $name;
       $this->course = $course;
   }
   public function showData() {
       echo $this->name." studies ".$this->course;
   }
}
$student = new Student;
$student->setData("Rahul", "BCA");
$student->showData();

Output

Rahul studies BCA

Here $this allows the object to store and retrieve its own data.

Conclusion

The $this keyword in PHP plays an important role in Object-Oriented Programming. It allows a class to access its own properties and methods using the current object reference.

Without $this, PHP cannot identify whether a variable or method belongs to the class or not. By using $this, developers can write clear, maintainable, and object-oriented code.

Note: It is important to remember that $this works only with object instances and cannot be used with static members.

PHP this keyword – Interview Questions

Q 1: What is $this keyword in PHP?
Ans: It refers to the current object instance.
Q 2: Where can $this be used?
Ans: Inside class methods.
Q 3: Can $this access private properties?
Ans: Yes.
Q 4: Can $this be used in static methods?
Ans: No.
Q 5: Why is $this important?
Ans: To access object properties and methods.

PHP this keyword – Objective Questions (MCQs)

Q1. $this refers to ______ in PHP.






Q2. $this can be used to access ______.






Q3. $this cannot be used in ______.






Q4. $this->property is used to ______.






Q5. $this is available only inside ______.






Related PHP Tutorials