Sobes.tech
Junior — Middle

What are the consequences of declaring a class with the final modifier?

sobes.tech AI

Answer from AI

Declaring a class with the final modifier in PHP means that this class cannot be inherited. It prevents the creation of subclasses and modification of the class's behavior through inheritance.

Example:

final class MyClass {
    public function sayHello() {
        echo "Hello!";
    }
}

// Attempting to inherit will cause an error
class ChildClass extends MyClass {} // Fatal error

Thus, final is used to protect the class from extension and to keep it unchanged.

What are the consequences of declaring a class with… - sobes.tech