What is self in PHP
In PHP, self
is a special keyword used within a class to refer to the class itself, not an object instance. Itβs typically used to access:
- Static properties
- Static methods
- Constants
- The current class scope (like in late static binding)
Example 1: Accessing a static property
class MyClass {
public static $name = "ChatGPT";
public static function getName() {
return self::$name; // Using self to access static property
}
}
echo MyClass::getName(); // Output: ChatGPT
Example 2: Accessing a class constant
class Config {
const SITE_NAME = "MySite";
public function showSiteName() {
return self::SITE_NAME; // Using self to access constant
}
}
β Difference from $this
Keyword | Refers to | Use case |
---|---|---|
$this | The current object | Access non-static properties/methods |
self | The class itself | Access static properties/methods/constants |
β οΈ Tip:
- If you’re doing inheritance and want the method/property from the child class, use
static::
(called late static binding) instead ofself::
.
Let’s walk through a comparison between self::
and static::
in PHP using a real inheritance example, so you can see why static::
(aka late static binding) is more flexible.
π Scenario:
You have a base class and a subclass. Each defines its own version of a static method or property.
β
self::
Example (Early binding β refers to the class where it’s defined)
class ParentClass {
public static function who() {
return "Parent";
}
public static function call() {
return self::who(); // Always refers to ParentClass::who()
}
}
class ChildClass extends ParentClass {
public static function who() {
return "Child";
}
}
echo ChildClass::call(); // Output: Parent β (not what we want)
π‘ Even though you called ChildClass::call()
, it uses self::who()
, which refers to ParentClass::who()
β not dynamic.
β
static::
Example (Late Static Binding β resolves to the calling class)
class ParentClass {
public static function who() {
return "Parent";
}
public static function call() {
return static::who(); // Will use the child class version if available
}
}
class ChildClass extends ParentClass {
public static function who() {
return "Child";
}
}
echo ChildClass::call(); // Output: Child β
π static::who()
defers resolution until runtime and correctly uses the overridden ChildClass::who()
method.
π§ Summary
Keyword | Behavior | Example Use |
---|---|---|
self:: | Early binding to the current class (where it is written) | Constants, base-only logic |
static:: | Late static binding β resolves to the class that was called at runtime | Extensible methods, polymorphism |
Early Static Binding (self::
)
It always refers to the current class where the code is written, even if you call it from a child class.
π§ Example:
class Animal {
public static function type() {
echo "Animal";
}
public static function sayType() {
self::type(); // This always calls Animal::type()
}
}
class Dog extends Animal {
public static function type() {
echo "Dog";
}
}
Dog::sayType(); // Output: Animal
β When to use:
- You want the same fixed behavior, no matter who calls it.
- You are writing a helper or utility method that should not change.
β
Late Static Binding (static::
)
It uses the class that actually calls it, so if a child class calls it, the childβs version will be used.
π§ Example:
class Animal {
public static function type() {
echo "Animal";
}
public static function sayType() {
static::type(); // This will call the type() of the class that calls it
}
}
class Dog extends Animal {
public static function type() {
echo "Dog";
}
}
Dog::sayType(); // Output: Dog
β When to use:
- You want child classes to override the behavior.
- You are building a base class, and you want the child class to customize it.
- Youβre using factory methods or static inheritance.
π§ Real-Life Analogy
self::
is like saying βI will always use my version of the tool, even if someone else calls me.βstatic::
is like saying βI will use your version of the tool, whoever calls me.β
π Simple Summary
Feature | self:: (Early) | static:: (Late) |
---|---|---|
Fixed to the base class | β Yes | β No |
Can be overridden by child | β No | β Yes |
Used for utility/helper | β Yes | β No |
Used in inheritance/factory | β No | β Yes |
Comments are closed.