Demonstrate self parent and keywords in terms of static members with examples in php
Sharing is Caring... Show some love :) Show
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:: AccessorIn order to picture this, let’s look at this line of code:
Looking at this example, it’s clear to see what the Using self:: with Static and Non-static Class MembersIt’s important to note that the Here are some code snippets to illustrate what this means:
In the sample above, we were able to call a static method from another static method using the As seen in the first code
snippet, we can also access static methods from other static methods using the
As seen in the example above, using a static method in a non-static method runs without problems.
Using a non-static method in a static method causes an error. The same rules applied here also apply for The parent:: AccessorThe parent:: accessor is a rather specific and exact keyword. It refers to methods and properties in a parent class from the scope of a child class. Since the child class should override the properties by default, the accessor can explicitly refer to the parent class’s property. It is also handy when both classes have properties with the same name.
The static:: AccessorIn order to see this, let’s look at this snippet of code:
As seen in this example, using the This happens because when we use Any method we access using the Take a break and check out the ultimate guide to Laravel 2021 to be up-to-date with Laravel Development. In PHP v5.3, a new feature called late static binding was implemented. The feature allows us to use the class named in the last call of a property. This means when you use the Here’s an example:
As seen in the code sample above, using the Caveat with static:: when using with private non-static methodsPrivate methods (and all properties) can only be accessed within the class that defines them. Parent classes can’t access them. When using the This is because when we access child (overridden) methods in child classes with the static:: keyword, we are trying to access them from the scope of the parent class. If the overridden method is private, at runtime, it will try to access the method it finds from the last class calling it (which is private) from the scope of the parent class, and since the parent class can’t access that method because it’s private, the code will fail. Here’s an example:
Using ConclusionWe learned a bit about inheritance and different accessors such as self::, static:: parent::, we can use when working with OOP in PHP. These accessors help us write clean code implementing the use of inheritance and polymorphism. With these, we can build efficient data structures that make our projects easy to build, scale, and understand even if we hand off our code to others. We hope we’ve helped you learn something new, and if you did, let us know in the comments. Catch you in the next one! What is parent and self in PHP?parent allows access to the inherited class, whereas self is a reference to the class the method running (static or otherwise) belongs to. A popular use of the self keyword is when using the Singleton pattern in PHP, self doesn't honour child classes, whereas static does New self vs.
What is self keyword in PHP?self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.
What is static keyword in PHP?The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
What is the difference between self and this keyword in PHP?The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.
|