Self vs. Static in PHP: Understanding the diffrence
Mar
15

Self vs. Static in PHP: Understanding the diffrence

In object-oriented programming, you'll often encounter the keywords self and static when accessing properties and methods within a class. Although they may appear similar, there's a crucial distinction between them. In this blog post, we'll dive into the functionalities of self and static explaining their differences and providing guidance on when to use each.

Understanding self

The self keyword refers to the class where it's defined, regardless of the context in which the method is called. It's resolved at compile time, meaning the reference is fixed during the code compilation phase. Here's what you can use self for:

  • Accessing static properties: self::$propertyName allows you to access static properties defined within the current class.
  • Calling static methods: Similar to properties, self::methodName() calls static methods belonging to the current class.

Consider this example:

<?php

class Animal {
  public static $species = "Unknown";

  public static function getName() {
    return "I am an animal of species " . self::$species;
  }
}

class Dog extends Animal {
  public static $species = "German Shepherd";
}

echo Dog::getName(); // Output: I am an animal of species Unknown

In this example, self::$species within the getName method of the Animal class refers to $species defined in the same class Animal , even when called from the Dog class.

Understanding static

The static keyword, on the other hand, exhibits late static binding. This means the reference is determined at runtime, based on the class from which the method is called. It's particularly useful for inheritance scenarios. Here's how static comes into play:

  • Accessing static properties: Similar to self, static::$propertyName accesses static properties. But with static, the property belongs to the class that actually calls the method.
  • Calling static methods: Likewise, static::methodName() calls static methods based on the calling class, enabling polymorphism.

Let's modify the previous example

class Animal {
  public static $species = "Unknown";

  public static function getName() {
    return "I am an animal of species " . static::$species;
  }
}

class Dog extends Animal {
  public static $species = "German Shepherd";
}

echo Dog::getName(); // Output: I am an animal of species German Shepherd

Now, with static::getName and static::$species in the Animal class, the call from Dog class points to $species defined in the Dog class, resulting in "German Shepherd" as the output.

Choosing between self and static

Now that you understand the core differences, here's a guideline for choosing between self and static:

  • Use self when you want to access static properties and methods that are defined in parent class, regardless of inheritance.
  • Use static when you require late static binding, allowing child classes to override static methods and properties inherited from the parent class.

In essence:

  • self for class-specific static elements (compile-time resolution).
  • static for polymorphism and runtime resolution based on the calling class. (runtime binding)

By understanding self vs. static in PHP, you can write cleaner, more maintainable code that leverages the power of object-oriented programming effectively.

 

 

 

Contact

Get in touch with us

Feel free to request missing tools or give some feedback.

Contact Us