Question
In PHP 5, what is the difference between using self and $this?
When should each one be used?
For example, how do they differ when accessing methods or properties inside a class?
Short Answer
By the end of this page, you will understand the difference between self and $this in PHP, how static context differs from instance context, and when each form should be used to access methods and properties inside a class.
Concept
self and $this are both used inside PHP classes, but they refer to different things.
$thisrefers to the current object instance.selfrefers to the current class itself.
This matters because PHP separates instance members from static members:
- Instance properties and methods belong to a specific object created from a class.
- Static properties and methods belong to the class itself, not to any one object.
$this means “this object”
Use $this when you want to work with data or methods that belong to a particular object.
class User {
public $name;
public function setName($name) {
$this->name = $name;
}
public function greet() {
return "Hello, " . $this->name;
}
}
Here, $this->name refers to the property of the current object.
Mental Model
Think of a class as a blueprint for houses, and objects as the actual houses built from that blueprint.
$thismeans: this specific house.selfmeans: the blueprint itself.
If you want to know the color of the kitchen in one house, you use $this because that belongs to a specific house.
If you want to read a rule written on the blueprint, such as a standard ceiling height shared by all houses, you use self.
So:
$this= object-level accessself= class-level access
Syntax and Examples
Core syntax
Accessing instance members with $this
class Person {
public $name = "";
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
- Use
->with$this - Works with non-static properties and methods
Accessing static members with self
class Counter {
public static $count = 0;
public static {
::++;
}
}
Step by Step Execution
Consider this example:
class ScoreBoard {
public $player;
public static $gamesPlayed = 0;
public function __construct($player) {
$this->player = $player;
self::$gamesPlayed++;
}
public function getPlayer() {
return $this->player;
}
public static function getGamesPlayed() {
return self::$gamesPlayed;
}
}
$game1 = new ScoreBoard("Anna");
$game2 = new ScoreBoard("Ben");
echo $game1->getPlayer();
::();
Real World Use Cases
$this and self appear often in real PHP applications.
Common uses of $this
Object state
class SessionUser {
public $email;
public function __construct($email) {
$this->email = $email;
}
}
Each logged-in user object has its own email.
Working with instance methods
class Order {
public function calculateTotal() {
return 100;
}
public function printInvoice() {
return "Total: " . $this->calculateTotal();
}
}
Real Codebase Usage
In real codebases, developers usually choose between $this and self based on whether they are writing object behavior or class-level behavior.
$this in application code
Instance services and models
class Product {
private $price;
public function __construct($price) {
$this->price = $price;
}
public function isExpensive() {
return $this->price > 100;
}
}
This is common in domain models, entities, and service objects.
Chaining methods
class QueryBuilder {
public function where(, ) {
;
}
}
Common Mistakes
1. Using $this inside a static method
This is a very common error.
Broken code
class Test {
public $name = "Sam";
public static function showName() {
return $this->name;
}
}
This fails because a static method does not have an object instance.
Fix
Either make the method non-static:
class Test {
public $name = "Sam";
public function showName() {
return $this->name;
}
}
Or make the property static if it truly belongs to the class:
class Test {
= ;
{
::;
}
}
Comparisons
| Concept | $this | self |
|---|---|---|
| Refers to | Current object instance | Current class |
| Used for | Non-static properties and methods | Static properties, static methods, constants |
| Syntax | $this->property, $this->method() | self::$property, self::method(), self::CONSTANT |
| Requires an object? | Yes | No |
| Available in static methods? | No | Yes |
| Shared across all objects? | No |
Cheat Sheet
Quick rules
- Use
$thisfor instance properties and methods - Use
selffor static properties, static methods, and constants $thisuses->selfuses::$thisis only available when an object existsselfworks in static methods
Syntax
$this->property;
$this->method();
self::$property;
self::method();
self::CONSTANT;
Remember
public $name;→ access with$this->namepublic static $count;→ access withself::$countconst VERSION = '1.0';→ access with
FAQ
What is the difference between self and $this in PHP?
$this refers to the current object instance. self refers to the current class and is used for static members and constants.
Can I use $this in a static method in PHP?
No. Static methods do not have an object instance, so $this is not available there.
When should I use self:: in PHP?
Use self:: when accessing static properties, static methods, or class constants from within the same class.
When should I use $this-> in PHP?
Use $this-> when accessing non-static properties or methods that belong to the current object.
Is self the same as a static property or static method?
No. self is the keyword used to refer to the class. It is commonly used to access static members, but it is not itself a property or method.
Can an instance method use self?
Yes. An instance method can access static members with , but it should use for instance members.
Mini Project
Description
Build a small PHP class that models a page visit tracker. This project demonstrates the difference between object-specific data and class-wide shared data. Each visit object should store its own page name, while the class should keep a shared count of how many visits have been created.
Goal
Create a class where $this stores per-object page data and self tracks the total number of visits across all objects.
Requirements
[ "Create a class with one instance property for the page name.", "Add one static property to count total visits.", "Increment the static counter whenever a new object is created.", "Add an instance method to return the page name.", "Add a static method to return the total number of visits." ]
Keep learning
Related questions
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.
Convert a PHP Object to an Associative Array
Learn how to convert a PHP object to an associative array, including quick methods, recursion, pitfalls, and practical examples.
Convert a Postman Request to cURL and PHP cURL
Learn how to convert a Postman POST request into a cURL command and use the same request in PHP cURL with headers and body.