Home > PHP, PHP Class Examples, PHP Tutorials, PHP5 OOPS Tutorials > PHP 5 Tutorial – Final Class and Methods

PHP 5 Tutorial – Final Class and Methods

February 14th, 2008 admin Leave a comment Go to comments

In this PHP tutorial we will understand the following:

  • Meaning of Final Class
  • Meaning of Final Method
  • When to declare a class as final
  • When to declare a method as final

Meaning of Final Class

A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below.

 
final class BaseClass {
   public function myMethod() {
      echo "BaseClass method called";
   }
}
 
//this will cause Compile error
class DerivedClass extends BaseClass {
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
$c = new DerivedClass();
$c->myMethod();

In the above example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.




Meaning of Final Method

A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the ‘final’ keyword. Example below:

 
class BaseClass {
   final public function myMethod() {
      echo "BaseClass method called";
   }
}
 
class DerivedClass extends BaseClass {
   //this will cause Compile error
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
$c = new DerivedClass();
$c->myMethod();

In the above example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.




When to declare a class as final

You should declare a class as final when you think that you implementation of that class should not change in the derived class. You should do this mainly for Utility classes where you don’t want the behavior/implementation of your class to change.

When to declare a method as final

You should declare a class method as final when you think that the method you develop contains necessary functionality to support your application and any modification or change to the functionality can cause unexpected errors/bugs.

Please feel free to write back for any clarification.

Subscribe to my free newsletter:

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – $this variable explained
  9. PHP5 Tutorial – instanceOf Operator Explained
  10. PHP5 Tutorial – Defining Class Constants
  11. PHP5 Tutorial – Inheritance
  12. PHP5 Tutorials – Abstract Class and Interface
  13. PHP5 OOPS Tutorials – Polymorphism
  14. PHP 5 Tutorials – Static Data Members and Methods
  15. PHP5 Tutorial – Magic Methods – __toString() method
  16. PHP5 Tutorial – Magic Methods – __get() and __set()
  17. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  18. PHP5 Tutorial – Magic Methods – __call() method
  19. PHP5 Tutorial – Magic Methods – __autoload() method
  20. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  21. PHP5 Tutorial – Magic Methods – __clone() method

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Amit
    April 18th, 2009 at 22:06 | #1

    Hello,

    When I am running the codes written above I neither get the output nor the result. What is reason behind it ? Please help to find the answer.

    Regards.

  2. admin
    April 19th, 2009 at 08:32 | #2

    Hi Amit,

    The program’s above will cause a compiler error. Check your error reporting settings in php.in files or set it to error_reporting(E_ALL) in your script and execute it again.

    The example actually shows that a final class and method cannot be overridden.

    Regards,
    Suniil
    http://www.careercurry.com – Great Career Articles and Advice

  3. Amit
    April 23rd, 2009 at 22:04 | #3

    Hi Sunil,

    I have add the statement you mentioned in my programming. But still it is not providing any error message.

    Also let me know something about php.ini file and how to tackle it and manage it efficiently.

    My code is as below.

    M1();

    ?>

    I have a request. I have few things to learn about Advanced PHP. As I found your way of teaching suits me I like to ask you. Should I ask on the Forum or there is a better place ? I will be obliged if you kindly help me.

    Regards.

  4. Amit
    April 23rd, 2009 at 22:07 | #4

    Hi Sunil,

    Please ignore the last message. I found the code is missing. Please do the needful.

    I have add the statement you mentioned in my programming. But still it is not providing any error message.

    Also let me know something about php.ini file and how to tackle it and manage it efficiently.

    My code is as below.

    M1();

    ?>

    I have a request. I have few things to learn about Advanced PHP. As I found your way of teaching suits me I like to ask you. Should I ask on the Forum or there is a better place ? I will be obliged if you kindly help me.

    Regards.

  5. Amit
    April 23rd, 2009 at 22:08 | #5

    Hello,

    Some problem is arising and hence I cannot add the code here. Please take a look.

    Regards.

  6. vigneshwaran
    November 25th, 2009 at 22:09 | #6

    gud but i want 1 mor example abt ths for better…..

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )