How to Create a Final Class in PHP

In this article, we show how to create a final class in PHP.
A final class is a class in which the class cannot be extended, meaning a child class cannot be created.
Just as the name final implies, it's a class that can't be manipulated. It's final.
So with a final class, no child class can be created from it.
So trying to use the keyword extends to create a child class will not work.
To create a final class in PHP, you simply have to put the keyword final before the keyword class.
So the following code below creates a final class named animals.
So the class above is a final PHP class.
No child class can be created from it.
If you attempt to create a child class from a final class, you will get a fatal error.
I took the above animals class and tried to create a subclass named zebras from it. I got an output shown as that shown below.
Fatal error: Class zebras may not inherit from final class (animals)
A final class is one where you absolutely do not want any inheritance. You do not want any child classes to inherit a final class.
In this way, a final class can be seen as a standalone class.
And making a class final, as you can see, is really simple. The final keyword simply needs to precede it.
Related Resources
How to Create a Final Method in PHP