In a lot of ways, PHP supports Object-Oriented Programming (especially using self::, static:: and parent:: in PHP). Show 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 0 method.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 3 accessor.ALSO READ Backend Development: The Ultimate Guide (2021) 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 5 and 6 in PHP.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 9 class, but that isn’t the case. ALSO READ How to be a better software developer This happens because when we use Any method we access using the 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 5 accessor, the method won’t be resolved until runtime, and during runtime, PHP will check for the last class that defines that method during the call. This would give us the kind of overriding mechanism we would expect. Here’s an example:
As seen in the code sample above, using the 5 makes the makeSound method to be accessed at runtime on the class that overrides it. That is why accessing vocalize from the different classes behaves the way we would expect it to.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 5 accessor to access private static methods in child classes, it will fail. ALSO READ Caching Strategy for RESTFUL API 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 6 or 7 in 8 would have worked without error, but it would echo “success!” and not “failure” because they would be referring to the 9 method in Class A, not the one in Class C. 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. |