M
M
mxdmg2017-08-02 00:36:47
PHP
mxdmg, 2017-08-02 00:36:47

Php trait overriding magic methods?

The essence of the question is in the title.
Is it possible and how to override the __construct __destruct magic methods in a class using the trait ?
for example, so that when the class dies, the class method is called first, then the method from the trait.
I didn't find it in the manual.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-08-02
@Sanasol

What is there to look for in the manual?
It's faster to write code and test.
You can use the magic method from traits.
They will be called if the class does not have its magic methods specified.
Class methods are called first, so overriding without changing the class will not work. Those. in class methods, you need to manually call the method from the trait.
In Google, right now, a bunch of everything was found for some reason.

N
Nazar Mokrinsky, 2017-08-02
@nazarpc

When using a trait, you can reassign the method name: php.net/manual/ru/language.oop5.traits.php#languag...
Thus, you can include a method __constructwith a different name and call it at the end of the class constructor. Likewise with any other methods. PHP does nothing automatically in this regard, all conflicts must be resolved explicitly, otherwise they will lead to an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question