Demonstrate self parent and keywords in terms of static members with examples in php

Sharing is Caring... Show some love :]

In a lot of ways, PHP supports Object-Oriented Programming [especially using self::, static:: and parent:: in PHP].

Seeing code written procedurally is becoming archaic and akin to finding dinosaur fossils in the Antarctic.

We can all agree that writing code in an Object-Oriented way is better than writing it procedurally in a lot of ways.

One of the fundamental tenets of an Object-Oriented Language is the concept of inheritance. A programming language must support inheritance to be seen as Object Oriented or maybe. 

When writing classes in PHP, there will be times when we need to access methods defined in a class or a parent class within the class itself [rather than an instance of the class]. 

There are three such accessors that can be easily confused, and these are self::, static:: and parent:: in PHP. This article will describe these in detail to remove any ambiguity that might exist with these accessors.

Before you dive in, check out the ultimate guide to Laravel 2021 to be up-to-date with Laravel Development.

If you’re a backend developer or looking at delving into this career path, join other developers to receive daily articles on backend development that will boost your productivity.

The self:: Accessor

In order to picture this, let’s look at this line of code:

Chủ Đề