N
N
Nikita Gusakov2013-08-02 23:48:44
PHP
Nikita Gusakov, 2013-08-02 23:48:44

How to change the parent of a class in php?

How, besides code substitution, to replace the parent of the class / change the behavior of the class? What about the built-in class?
In my particular case - PDO, PDOStatement. I wrote a small wrapper that works conveniently with placeholders, I want to write tests, but I don’t understand how to do it at all.
The wrapper complements the existing methods, almost does not use its own. Of course, you can move your code into your methods, but this is at least an extra function call, and will be done only for the sake of testing.
github.com/hell0w0rd/dbal/tree/master/src/Fountain/Dbal
- here is a link to the repository to talk in detail functions.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nazar Mokrinsky, 2013-08-03
@hell0w0rd

But what if you inherit not from the standard, but from your own, which in turn inherits the standard

class custom_PDOStatement extends \PDOStatement {}

Then custom_PDOStatement can be substituted for the test.

E
EugeneOZ, 2013-08-03
@EugeneOZ

Prefer composition over inheritance and you won't have these problems.

but this is at least an extra function call, and will be done only for the sake of testing

and there is nothing wrong with that.
inheritance is not such a cheap procedure, or is everything ok?

not there you measure productivity, not there at all. All this in the opcode cache will be a meager time. Most of the time will be spent working with the database, disk and network.

O
OnYourLips, 2013-08-03
@OnYourLips

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('custom_PDOStatement', array($pdo)));

V
Vladimir Chernyshev, 2013-08-06
@VolCh

www.php.net/manual/en/function.runkit-class-adopt.php - isn't it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question