PHP final Classes and Methods

Why

To lock down a class so that it can't be inherited from. Or to lock down one or more methods inside a class so that they can't be overridden in a child class.

By doing this, you know that your class or methods within your class will always behave in exactly the same way.

Syntax

You can add the keyword final before a class or method definition to lock down that class or method.

Here's how to create a final class:


final class MyClass { 

} 

Here's how you make a final method:


class ParentClass { 
   public final function myMethod() { 
            echo "A method"; 
   } /*ww  w  .ja va  2s . c  o  m*/
} 




















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition