2

When I read the limitation of Plugin in Magento2, the official devdocs mentions that,

Plugins can not be used on following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct and __destruct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

May I know what is the meaning of Final methods and Final classes ?

1 Answer 1

4
Final is a keyword used in class and methods

Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

For example

We can't override the parent class function.

<?php
   class BaseClass{
      final function calculate($val1,$val2){
         $sum = $val1+$val2;
         echo "Sum of given no=".$sum;
         }
      }
   class ChildClass extends BaseClass{
      function calculate($x,$y){
         $mult=$val1*$val2;
         echo "Multiplication of given no=".$mult;
         }
      }
   $obj= new ChildClass();
   $obj->show(10,10);
?>

Output: PHP Fatal error: Cannot override final method BaseClass::calculate()

You can find it Here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.