DEV Community

Cover image for Understanding $this, self, parent, and static keywords in PHP and Laravel
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Edited on

Understanding $this, self, parent, and static keywords in PHP and Laravel

Hello artisans,

In the programming world of PHP, understanding the subtleties of object-oriented concepts can greatly enhance your ability to write efficient and maintainable code.

Among these concepts, keywords like $this, self, parent, and static hold significant importance, especially in frameworks like Laravel where object-oriented principles are heavily utilised.

In this blog post, we'll delve into these keywords, exploring their roles and usage in plain PHP and the Laravel framework.

The $this keyword: $this

The $this keyword refers to the current object instance within a class context. It allows you to access the properties and methods of the current object.
In PHP, $this is used to refer to the current object's properties and methods from within the class definition.

class Message {
    public $greeting = "Hello developers";

    public function getGreetingMessage() {
        return $this->greeting;
    }
}

$obj = new Message();
echo $obj->getGreetingMessage(); // Outputs: Hello developers
Enter fullscreen mode Exit fullscreen mode

The static Context: self and static

The self keyword is used to access static members (properties and methods) of a class from within the class itself. On the other hand, the static keyword is used to reference the called class in late static binding contexts.

class Counter {
    public static $count = 0;

    public function __construct() {
        self::$count++;
    }

    public static function getCount() {
        return self::$count;
    }
}

$counterObj1 = new Counter();
$counterObj2 = new Counter();
echo Counter::getCount(); // Outputs: 2
Enter fullscreen mode Exit fullscreen mode

Accessing Parent Class: parent

The parent keyword allows access to properties and methods of the parent class from within a child class. It's particularly useful for invoking overridden parent methods or accessing parent class members.

class ParentClass {
    protected $message = "I am from Parent class";

    public function getMessage() {
        return $this->message;
    }
}

class ChildClass extends ParentClass {
    public function getParentMessage() {
        return parent::getMessage();
    }
}

$obj = new ChildClass();
echo $obj->getParentMessage(); // Outputs: I am from Parent class
Enter fullscreen mode Exit fullscreen mode

Laravel's Usage

In Laravel, understanding these keywords becomes crucial for leveraging the framework's features effectively. For instance, $this is used extensively within controllers to access services and dependencies. Similarly, self and static are utilized for defining and accessing static properties and methods, especially in model classes and service providers.

Additionally, parent is used in Laravel's Eloquent ORM for calling parent class methods and properties, enabling developers to customise and extend Eloquent's functionality seamlessly.

Conclusion

Understanding $this, self, parent, and static keywords in PHP and Laravel is fundamental for writing robust and maintainable object-oriented code. By grasping the nuances of these keywords, developers can enhance their proficiency in PHP programming and leverage the full potential of frameworks like Laravel.

If you enjoy reading my blog posts and find them helpful, consider supporting my work by buying me a coffee!

Buy Me A Coffee

Happy Reading ❤️ 🦄

Top comments (2)

Collapse
 
fullfull567 profile image
fullfull567

Just came across this super helpful article while diving into PHP. 🚀 Kudos to the author – your insights really made a difference for a newbie like me!

Collapse
 
snehalkadwe profile image
Snehal Rajeev Moon

Thank you so much for your kind words! I'm thrilled to hear that you found the article helpful on your PHP journey. If you have any questions or need further clarification, feel free to ask. Happy coding! 🚀