I just went through this exact thing. You don't need to extend it. Make a class that holds SimpleXMLElement objects. I believe this is what Nikola meant.

class XmlResultSet
{
    public $xmlObjs = array();

    public function __construct(array $xmlFiles)
    {
      foreach ($xmlFiles as $file) {
          $this->xmlObjs[] = new XmlResult($file);
      }
    }
}

class XmlResult
{
    private $xmlObj;

    public function __construct($file)
    {
        try {
            $this->xmlObj = new SimpleXMLElement($file, 0, true);
        }
        catch (Exception $e) {
            throw new MyException("Invalid argument ($this)($file)(" . $e .
            ")", PHP_ERRORS);
        }
    }

    public function otherFunctions()
    {
        return $this->xmlObj->movie['name']; // whatever
    }
}
Answer from vcardillo on Stack Overflow
🌐
PHP
php.net › manual › en › language.oop5.final.php
PHP: Final Keyword - Manual
The final keyword prevents child classes from overriding a method, property, or constant by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
🌐
Matthias Noback
matthiasnoback.nl › 2018 › 09 › final-classes-by-default-why
Final classes by default, why? | Matthias Noback
September 11, 2018 - After explaining good reasons for ... it “final”. PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended....
🌐
Mark Baker's Blog
markbakeruk.net › 2017 › 11 › 19 › extending-final-classes-and-methods-by-manipulating-the-ast
Extending final Classes and Methods by manipulating the AST | Mark Baker's Blog
November 19, 2017 - As a first step, I want to traverse the AST looking for the code for the class that I’m interested in mocking, “de-finalising” it if it’s defined as final, and the same for any methods in that class. So I start by writing my NodeVisitor. use PhpParser\NodeVisitorAbstract; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; $nodeVisitor = new class extends NodeVisitorAbstract { protected $parsingTargetClass = false; public $className; public function enterNode(Node $node) { if (($node instanceof ClassMethod) && $node->isPublic() && $this->parsingTarg
🌐
Verraes
verraes.net › 2014 › 05 › final-classes-in-php
Final Classes: Open for Extension, Closed for Inheritance
May 12, 2014 - Composition, strategies, callbacks, plugins, event listeners, ... are all valid ways to extend without inheritance. ... I make all my classes final by default. I even configured the templates in my IDE prefix new classes with ‘final’. I’ve often explained my reasoning to people.
🌐
idswater
ids-water.com › 2019 › 11 › 15 › can-we-extend-a-final-class-in-php
Can we extend a final class in PHP? – idswater.com
PHP Final Class works based on the declaration just by adding the FINAL keyword before the class word. Final Class works without extending the class. Child class can’t override the final methods. Normal class variables cannot be used to declare as final.
🌐
W3Schools
w3schools.com › php › keyword_extends.asp
PHP extends Keyword
json_decode() json_encode() PHP Keywords · abstract and as break callable case catch class clone const continue declare default do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile extends final finally fn for foreach function global if implements include include_once instanceof insteadof interface isset list namespace new or print private protected public require require_once return static switch throw trait try use var while xor yield yield from PHP Libxml ·
Top answer
1 of 7
72

I have often read that classes should only be made 'final' on rare/special occasions.

Whoever wrote that is wrong. Use final liberally, there’s nothing wrong with that. It documents that a class wasn’t designed with inheritance in mind, and this is usually true for all classes by default: designing a class that can be meaningfully inherited from takes more than just removing a final specifier; it takes a lot of care.

So using final by default is by no means bad. In fact, amongst OOP experts it’s widely agreed that final should be the default, e.g. Jon Skeet:

Classes should be sealed by default in C#

Or Joshua Bloch:

Design and document for inheritance or else prohibit it [Effective Java, 3rd Ed, Item 19]

Or Scott Meyers [More Effective C++, Item 33].

Which is why modern OO langauges such as Kotlin have final-by-default classes.

You wrote:

Maybe it screws up Mock object creation …

And this is indeed a caveat, but you can always recourse to interfaces if you need to mock your classes. This is certainly superior to making all classes open to inheritance just for the purpose of mocking.

2 of 7
8

If you want to leave a note to yourself that a class has no sub-classes, then by all means do so and use a comment, thats what they are for. The "final" keyword is not a comment, and using language keywords just to signal something to you (and only you would ever know what it means) is a bad idea.

edited by original author to add: I completely disagree with this now. I cannot even create a model of my mental state 11 years ago that would explain why I would say this. I think this answer, and my comments defending it below, are ridiculous. The accepted answer is right.

🌐
Medium
medium.com › @rcsofttech85 › final-classes-and-the-power-of-composition-in-php-205095193f05
Final Classes and the Power of Composition in PHP | by rahul chavan | Medium
March 22, 2024 - In PHP, a class declared as final cannot be subclassed, meaning it cannot have child classes. This prevents developers from extending or modifying the behavior of the final class through inheritance.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › explain-final-class-and-final-method-in-php
Explain final class and final method in PHP.
In some cases, we might need to ... can be achieved with the final, by prefixing the class and function with the final keyword.which essentially causes PHP to produced an error in the event that anybody attempts to extend a final class or override final function....
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php final class
PHP Final Class | How PHP Final Class Works with Advantages
March 28, 2023 - PHP Final Class works based on the declaration just by adding the FINAL keyword before the class word. Final Class works without extending the class. Child class can’t override the final methods.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GeeksforGeeks
geeksforgeeks.org › php › what-are-the-final-class-and-final-method-in-php
What are the final class and final method in PHP ? - GeeksforGeeks
July 23, 2025 - Fatal error:Cannot override final method ParentGFG::print() in /home/c055b5cf6357c8cee93036f29c4c2eb8.php on line 11 ... The advantage of declaring a class or method as final is it increases the security of class and methods by preventing overriding. Both are non-extendable.
🌐
Coursesweb
coursesweb.net › php-mysql › php-oop-final-classes-methods
PHP OOP - Final Classes and Methods
<?php // base class class Base { // final method final public function testMethod() { echo 'This is a final method'; } } // define a child class derivated from Base class BaseChild extends Base { // override the testMethod() public function testMethod() { echo 'Another text'; } } ?> This example will generate the following error:
🌐
Hacking with PHP
hackingwithphp.com › 6 › 7 › 4 › final
Final – Hacking with PHP - Practical PHP
<?php final class dog { public $Name; private function getName() { return $this->Name; } } class poodle extends dog { public function bark() { print "'Woof', says " .
🌐
DEV Community
dev.to › erlandmuchasaj › final-and-readonly-classes-in-php-38fi
Final and Readonly Classes in PHP - DEV Community
March 21, 2023 - <?php namespace App\Utils; class ChildClass extends ParentClass { } So we have a ParentClass and a ChildClass that extends the parent. ... And this would output ChildClass public properties and also inherited properties from ParentClass as follows: Now if we do not want our class to be extended we just put the word final at the beginning of the class as follows:
🌐
Matthewdaly
matthewdaly.co.uk › blog › 2023 › 08 › 13 › why-nearly-every-php-class-you-write-should-be-abstract-or-final
Why (nearly) every PHP class you write should be abstract or final | Matthew Daly
It allowed you to easily extend existing code with minimal effort, and bend existing third-party packages to suit your needs. However, after nearly twelve years in the industry, I've come to the conclusion that in fact, inheritance is only a comparatively fringe benefit of object-oriented PHP. Nowadays, I declare most classes I write as final...
🌐
GitHub
github.com › php-ds › ext-ds › issues › 54
final classes and extend · Issue #54 · php-ds/ext-ds
April 1, 2016 - Hi, Is there a compeling reason to make all classes final? It makes it hard to do this; class SomeSet extends Set { ... } function someFunction(SomeSet $set) { ... } I know I can do composition over inheritance to create VO that use ds i...
Published   Aug 31, 2016
🌐
GitHub
ocramius.github.io › blog › when-to-declare-classes-final
When to declare classes final
There are numerous reasons to mark a class as final: I will list and describe those that are most relevant in my opinion. Developers have the bad habit of fixing problems by providing specific subclasses of an existing (not adequate) solution. You probably saw it yourself with examples like following: <?php class Db { /* ... */ } class Core extends Db { /* ...