PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Final Classes And Final Methods

In PHP, the final keyword is used to prevent class inheritance (when applied to a class) and to prevent method overriding (when applied to a method).

Final Classes

When a class is declared as final, it cannot be extended by other classes. Here's an example:

final class FinalClass {
    // Class properties and methods go here
}

class SomeClass extends FinalClass { 
    // Results in Fatal error: Class SomeClass may not inherit from final class (FinalClass)
}

In this example, because FinalClass is declared as final, trying to extend it in SomeClass results in a fatal error.

Final Methods

When a method in a class is declared as final, it cannot be overridden in a child class. Here's an example:

class ParentClass {
    final public function myMethod() {
        echo 'Parent';
    }
}

class ChildClass extends ParentClass {
    public function myMethod() { 
        // Results in Fatal error: Cannot override final method ParentClass::myMethod()
        echo 'Child';
    }
}

In this example, because myMethod() in ParentClass is declared as final, trying to override it in ChildClass results in a fatal error.

The final keyword is useful when you want to ensure that a class maintains its original behavior, and is not extended or modified by any other class. Similarly, marking a method as final ensures that any classes extending the parent class will use the parent method as-is, without any modifications.

Remember that using final increases rigidity, as it prevents other developers from extending your classes or methods. It should be used judiciously and only when necessary.

  1. When to use final classes in PHP:

    • Use final classes when you want to prevent the class from being extended by other classes.
    • Final classes are useful when you want to enforce that a class should not have any subclasses.
    <?php
    final class MyFinalClass {
        // Class implementation
    }
    
  2. Benefits of using final methods in PHP:

    • Final methods prevent subclasses from overriding the method.
    • Useful when you want to ensure that a specific method should not be modified in any subclass.
    <?php
    class ParentClass {
        final public function myFinalMethod() {
            // Method implementation
        }
    }
    
    class ChildClass extends ParentClass {
        // Attempting to override myFinalMethod will result in an error
    }
    
  3. Examples of PHP final classes and methods:

    • Example of a final class:
    <?php
    final class MyFinalClass {
        // Class implementation
    }
    
    • Example of a final method:
    <?php
    class ParentClass {
        final public function myFinalMethod() {
            // Method implementation
        }
    }
    
  4. Overriding and extending final methods in PHP:

    • Final methods cannot be overridden in subclasses. Attempting to override a final method will result in an error.
    <?php
    class ParentClass {
        final public function myFinalMethod() {
            // Method implementation
        }
    }
    
    class ChildClass extends ParentClass {
        // Attempting to override myFinalMethod will result in an error
    }
    
  5. PHP inheritance with final classes and methods:

    • Final classes cannot be extended, and final methods cannot be overridden, ensuring that the behavior defined in the class remains unchanged.
    <?php
    final class MyFinalClass {
        final public function myFinalMethod() {
            // Method implementation
        }
    }