K
K
Karponter2015-09-25 01:54:46
PHP
Karponter, 2015-09-25 01:54:46

How to bypass the final constructor?

In general, I decided to slightly expand the standard SimpleXMLElement for my own needs, and I need to call the constructor of the parent class SimpleXMLElement when initializing the object.
But it wasn’t there: the constructor of the parent class turned out to be final, and it’s impossible to overload it with the constructor of the heir.
Question: how to bypass?

class MyClass extends SimpleXMLElement {
  function __construct() {
    parent::__construct('<data></data>');
  }
}

PHP Fatal error: Cannot override final method SimpleXMLElement::__construct()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-09-25
@karponter

The first comment in the documentation already answers your question.
php.net/manual/ru/simplexmlelement.construct.php#79310
https://ru.wikipedia.org/wiki/Delegation_Pattern

N
Nazar Mokrinsky, 2015-09-25
@nazarpc

class MyClass extends SimpleXMLElement {
  function create_instance() {
    $instance = new self('<data></data>');
    ...
    return $instance;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question