final keyword in php | final keyword in php with example | final keyword in php oops | final keyword in php method | use of final keyword in php | why do we use final keyword in php

The final keyword in PHP professional assets skill

This article sorts out your doubts about the final keyword in PHP. Java developer is very well familiar with the Final. In PHP, we have the Final identically. 

But before diving into this post. Let me make it clear. In PHP, we can’t declare the class property as final. We use Const for a class property. 

We Only can declare classes and methods as final. As we know Inheritance, it’s a great feature of object-oriented programming. It is essential and helpful. Sometimes it becomes problematic for large applications. To overcome this problem, we have a Final keyword.

Why do we use the Final keyword in PHP?

We use the Final keyword in such a scenario when we don’t want to inherit our parent class by its child or when we don’t want the parent method override by its child class method.

Before progressing further with this discussion, I would like to acknowledge the use of the Final keyword. I will explain more about it with an example below.

  • Prevent class inheritance.
    • In any case, if you don’t want your class to retouch, make it final. In PHP this is how we use final keyword in class.
<?php

final class notExtendableFinalClass
{
	public $productID;
	
	public function getProductID()
	{
		return $this->productID;
	}
}

class tryingToExtentFinalClass extends notExtendableFinalClass{
	
}

If you would run the above code it would give you error.

Fatal error: Class tryingToExtentFinalClass may not inherit from final class (notExtendableFinalClass) in C:\xampp\htdocs\final.php on line 13

  • Prevent class method from being overridden.
    • To prevent child classes from overriding the parent Class Method, we should use the Final keyword. 
<?php
class notExtendableFinalClass
{
	public $productID;
	
	final public function getProductID()
	{
		return $this->productID;
	}
}

class tryingToExtentFinalClass extends notExtendableFinalClass{
	public function getProductID(){}
}

Fatal error: Cannot override final method notExtendableFinalClass::getProductID() in C:\xampp\htdocs\webylava.com\final.php on line 13

So in this way we have seen that how the final keyword is used to prevent class inheritance and method overriding. We learned the usage of the Final.

Sometimes you notice Static keywords. We use the Static keyword when we don’t want to instantiate an object. Always remember, a Class can not be static, unlike Final. Static variables and methods are common to all the objects of the class.

Can we make constructor Final?

No, we should not define a Final constructor even there is no need of static constructor.

An image illustration to explain better

final keyword in php with example | final keyword in php tutorial | final keyword in class in php

HOME

1 thought on “The final keyword in PHP professional assets skill”

  1. Pingback: php detect https code 2021 - PHPPanda.com

Leave a Comment

Your email address will not be published. Required fields are marked *