🐘 PHP Beginner

What is $this in PHP?

Why Interviewers Ask This

This is a classic screening question for PHP roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

$this is a pseudo-variable available inside non-static class methods that refers to the current object instance being operated on. It is used to access instance properties and methods: $this->name accesses the name property, and $this->getName() calls the getName() method. $this is not available in static methods because static methods belong to the class, not an instance. Attempting to use $this in a static context produces a notice. The equivalent for referencing the class itself (including static properties and methods) is the self::, static::, and parent:: keywords.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex PHP answers easy to follow.